ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Keith Bennett : What I Love About Ruby

This page last changed on Sep 17, 2008 by kbennett.


What I Love About Ruby
Keith Bennett

kbennett .at. bbsinc .dot. biz




Simplicity of Creating Instance Variables with Accessors and Mutators in Ruby

class Y
  attr_accessor :a
end


...creates an instance variable a, and an accessor and mutator.


Concise Idiom for Conditional (and Lazy) Initialization

@var ||= some_expensive_initialization


...means if var is undefined, define it, and if nil, do the initialization.


Numeric Constants Thousands Separators Supported

irb(main):002:0> 1_000_000
=> 1000000
irb(main):003:0> 1_000_000.class
=> Fixnum


Actually, all underscores are stripped, even if they do not separate thousands.


Shell Integration

A shell command enclosed in backticks will be run, and the value returned by the backticked command
will be the text the command sent to stdout:

irb(main):008:0>         `mkdir a b c d`
=> ""
irb(main):009:0>         `touch b/foo d/foo`
=> ""
irb(main):010:0>         emptydirs = `find . -type d -empty`
=> "./an./cn"
irb(main):011:0>         puts emptydirs
./a
./c
=> nil



Logical Syntax:

1.upto(10) { |i| puts i }

(100..200).each { |n| puts n }




Document generated by Confluence on Sep 18, 2008 09:45                                          Page 1
vs., in Java, for the first example:

for (int i = 0; i <= 10; i++) {
  System.out.println(i) ;
}



Ability to Specify Arrays (and Hashes) as Literals

and the Ease of Iterating Over Them

irb(main):018:0> ['collie', 'labrador', 'husky'].each { |breed|
  puts "Hi, I'm a #{breed}, and I know how to bark."
}
Hi, I'm a collie, and I know how to bark.
Hi, I'm a labrador, and I know how to bark.
Hi, I'm a husky, and I know how to bark.
=> ["collie", "labrador", "husky"]


Also:

%w(collie labrador husky)


can be used to create the array instead of:

['collie', 'labrador', 'husky']


A Hash:



irb(main):063:0> favorites = { :fruit => :durian, :vegetable => :broccoli }
=> {:fruit=>:durian, :vegetable=>:broccoli}



Ranges

water_liquid_range = 32.0...212.0
=> 32.0...212.0
irb(main):010:0> water_liquid_range.include? 40
=> true
irb(main):011:0> water_liquid_range.include? -40
=> false


Note: Ranges are not arrays; any number n, not just integers, such that 32.0 <= n < 212.0, is included
in the range.


Converting Ranges to Arrays:



irb(main):043:0> ('m'..'q').to_a
=> ["m", "n", "o", "p", "q"]



Blocks Used to Automatically Close Resources

File.open 'x.txt', 'w' do |file|
  file << 'Hello, world'




Document generated by Confluence on Sep 18, 2008 09:45                                            Page 2
end


The file is automatically closed after the block completes. If no block is provided, then the open function
returns the file instance:

irb(main):001:0>   f = File.open 'x.txt', 'w'
=> #<File:x.txt>
irb(main):002:0>   f << "Pleaaase, delete me, let me go..."
=> #<File:x.txt>
irb(main):003:0>   f.close
=> nil
irb(main):004:0>   puts IO.read('x.txt')
Pleaaase, delete   me, let me go...
=> nil



Simple File Operations

file_as_lines_array = IO.readlines 'x.txt'
file_as_single_string = IO.read 'x.txt'



Clean and Simple Syntax

puts Array.instance_methods.sort



Regular Expressions

irb(main):027:0>   'ruby' =~ /ruby/
=> 0
irb(main):028:0>   'rubx' =~ /ruby/
=> nil
irb(main):029:0>   'ruby' =~ /Ruby/
=> nil
irb(main):030:0>   'ruby' =~ /Ruby/i
=> 0



Arrays:

irb(main):001:0> nums = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):006:0> nums.include? 3
=> true
irb(main):004:0> nums.collect { |n| n * n }
=> [1, 4, 9, 16, 25]
irb(main):002:0> nums.reject { |n| n % 2 == 0}
=> [1, 3, 5]
irb(main):003:0> nums.inject { |sum,n| sum += n }
=> 15
irb(main):052:0* distances_in_miles = [10, 50]=> [10, 50]
irb(main):053:0> distances_in_km = distances_in_miles.map { |n| n * 9.0 / 5.0 }
=> [18.0, 90.0]irb(main):016:0* twos = (0..10).map { |n| n * 2 }
=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
irb(main):017:0> fours = (0..5).map { |n| n * 4 }
=> [0, 4, 8, 12, 16, 20]
irb(main):018:0> twos - fours
=> [2, 6, 10, 14, 18]
irb(main):019:0> twos & fours


Document generated by Confluence on Sep 18, 2008 09:45                                                Page 3
=> [0, 4, 8, 12, 16, 20]
irb(main):020:0> fours * 2
=> [0, 4, 8, 12, 16, 20, 0, 4, 8, 12, 16, 20]
irb(main):021:0> twos + fours
=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 0, 4, 8, 12, 16, 20]



Built-in String Operations

   ?   Case Conversions, Capitalization
   ?   Left, Right, Sub
   ?   Strip, Justify, Center
   ?   Search and Replace (gsub)
   ?   Insert, Delete




Document generated by Confluence on Sep 18, 2008 09:45            Page 4

More Related Content

What's hot (13)

mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
Tse-Ching Ho
?
¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ
¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ
¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ
Takahiro Inoue
?
20110514 mongo db¥Á¥å©`¥Ë¥ó¥°
20110514 mongo db¥Á¥å©`¥Ë¥ó¥°20110514 mongo db¥Á¥å©`¥Ë¥ó¥°
20110514 mongo db¥Á¥å©`¥Ë¥ó¥°
Yuichi Matsuo
?
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDBScala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
jorgeortiz85
?
20171014 tips for manipulating filesystem in julia
20171014 tips for manipulating filesystem in julia20171014 tips for manipulating filesystem in julia
20171014 tips for manipulating filesystem in julia
ÔÀÈA ¶Å
?
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberlCukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Skills Matter
?
[131]??? ???? ????
[131]??? ???? ????[131]??? ???? ????
[131]??? ???? ????
NAVER D2
?
±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö
±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö
±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö
Takahiro Inoue
?
Undrop for InnoDB
Undrop for InnoDBUndrop for InnoDB
Undrop for InnoDB
Aleksandr Kuzminsky
?
Solr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene EuroconSolr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene Eurocon
Giovanni Fernandez-Kincade
?
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
Basuke Suzuki
?
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
markstory
?
High performance GPU computing with Ruby RubyConf 2017
High performance GPU computing with Ruby  RubyConf 2017High performance GPU computing with Ruby  RubyConf 2017
High performance GPU computing with Ruby RubyConf 2017
Prasun Anand
?
¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ
¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ
¤Ï¤¸¤á¤Æ¤Î²Ñ´Ç²Ô²µ´Ç¶Ùµþ
Takahiro Inoue
?
20110514 mongo db¥Á¥å©`¥Ë¥ó¥°
20110514 mongo db¥Á¥å©`¥Ë¥ó¥°20110514 mongo db¥Á¥å©`¥Ë¥ó¥°
20110514 mongo db¥Á¥å©`¥Ë¥ó¥°
Yuichi Matsuo
?
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDBScala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
jorgeortiz85
?
20171014 tips for manipulating filesystem in julia
20171014 tips for manipulating filesystem in julia20171014 tips for manipulating filesystem in julia
20171014 tips for manipulating filesystem in julia
ÔÀÈA ¶Å
?
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberlCukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Skills Matter
?
[131]??? ???? ????
[131]??? ???? ????[131]??? ???? ????
[131]??? ???? ????
NAVER D2
?
±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö
±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö
±á²¹»å´Ç´Ç±è¤È²Ñ´Ç²Ô²µ´Ç¶Ùµþ¤ò»îÓä·¤¿¥½©`¥·¥ã¥ë¥¢¥×¥ê¤Î¥í¥°½âÎö
Takahiro Inoue
?
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
markstory
?
High performance GPU computing with Ruby RubyConf 2017
High performance GPU computing with Ruby  RubyConf 2017High performance GPU computing with Ruby  RubyConf 2017
High performance GPU computing with Ruby RubyConf 2017
Prasun Anand
?

Viewers also liked (19)

Earth & Sky Photo Contest 2016: Winners
 Earth & Sky Photo Contest 2016: Winners Earth & Sky Photo Contest 2016: Winners
Earth & Sky Photo Contest 2016: Winners
maditabalnco
?
A beleza da mulher woman beauty
A beleza da mulher   woman beautyA beleza da mulher   woman beauty
A beleza da mulher woman beauty
Nelita Lisboeta
?
Report on Blog User in Indonesia 2013
Report on Blog User in Indonesia 2013Report on Blog User in Indonesia 2013
Report on Blog User in Indonesia 2013
MACROMILL SOUTH EAST ASIA, INC.
?
Workshop if technology
Workshop if technologyWorkshop if technology
Workshop if technology
Martijn Bloksma
?
World Economic Forum on Africa 2006
World Economic Forum on Africa 2006World Economic Forum on Africa 2006
World Economic Forum on Africa 2006
WorldEconomicForumDavos
?
§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó
§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó
§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó
pedromans
?
2nd Annual M2M and IoT Strategies Summit - production-1-new brochure-2
2nd Annual M2M and IoT Strategies Summit - production-1-new brochure-22nd Annual M2M and IoT Strategies Summit - production-1-new brochure-2
2nd Annual M2M and IoT Strategies Summit - production-1-new brochure-2
Jorge Rivero Sanchez
?
Talking to kids Nannypalooza 2015
Talking to kids Nannypalooza 2015Talking to kids Nannypalooza 2015
Talking to kids Nannypalooza 2015
William Sharp
?
The disruptive-power-of-virtual-currency
The disruptive-power-of-virtual-currencyThe disruptive-power-of-virtual-currency
The disruptive-power-of-virtual-currency
Aranca
?
Navigating the Flood of BYOD
Navigating the Flood of BYODNavigating the Flood of BYOD
Navigating the Flood of BYOD
LiveAction Next Generation Network Management Software
?
§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç
§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç
§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç
Promodo
?
Accessibility First
Accessibility FirstAccessibility First
Accessibility First
Ramses Cabello
?
Beyond Ad-hoc Automation: Leveraging Structured Platforms
Beyond Ad-hoc Automation: Leveraging Structured PlatformsBeyond Ad-hoc Automation: Leveraging Structured Platforms
Beyond Ad-hoc Automation: Leveraging Structured Platforms
bridgetkromhout
?
20 Survival Tips For Virgin Founders
20 Survival Tips For Virgin Founders20 Survival Tips For Virgin Founders
20 Survival Tips For Virgin Founders
Vitaly Golomb
?
MMA F¨®rum Brasil 2015MMA F¨®rum Brasil 2015
MMA F¨®rum Brasil 2015
Mobile Marketing Association
?
Purpose Driven Student Leadership for Civic Engagement
Purpose Driven Student Leadership for Civic EngagementPurpose Driven Student Leadership for Civic Engagement
Purpose Driven Student Leadership for Civic Engagement
Matt Cummings
?
Plant Manager Resume
Plant Manager ResumePlant Manager Resume
Plant Manager Resume
Curtis Turner
?
ISBN - Jenn Lim (Rancho Palos Verdes, CA)
ISBN - Jenn Lim (Rancho Palos Verdes, CA)ISBN - Jenn Lim (Rancho Palos Verdes, CA)
ISBN - Jenn Lim (Rancho Palos Verdes, CA)
Delivering Happiness
?
SPT 101 - Office 365 and Hybrid Solutions
SPT 101 - Office 365 and Hybrid SolutionsSPT 101 - Office 365 and Hybrid Solutions
SPT 101 - Office 365 and Hybrid Solutions
Dan Usher
?
Earth & Sky Photo Contest 2016: Winners
 Earth & Sky Photo Contest 2016: Winners Earth & Sky Photo Contest 2016: Winners
Earth & Sky Photo Contest 2016: Winners
maditabalnco
?
A beleza da mulher woman beauty
A beleza da mulher   woman beautyA beleza da mulher   woman beauty
A beleza da mulher woman beauty
Nelita Lisboeta
?
§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó
§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó
§®§à§ß§Ú§ä§à§â§Ú§ß§Ô³å§ä§â§Ñ§ã§á§à§â§ä§ß§í§ç³å§á§à§ä§à§Ü§à§Ó³å§ß§Ñ³å§à§ã§ß§à§Ó§Ö³å§Õ§Ñ§ß§ß§í§ç³å§ã§à§ä§à§Ó§í§ç³å§à§á§Ö§â§Ñ§ä§à§â§à§Ó
pedromans
?
2nd Annual M2M and IoT Strategies Summit - production-1-new brochure-2
2nd Annual M2M and IoT Strategies Summit - production-1-new brochure-22nd Annual M2M and IoT Strategies Summit - production-1-new brochure-2
2nd Annual M2M and IoT Strategies Summit - production-1-new brochure-2
Jorge Rivero Sanchez
?
Talking to kids Nannypalooza 2015
Talking to kids Nannypalooza 2015Talking to kids Nannypalooza 2015
Talking to kids Nannypalooza 2015
William Sharp
?
The disruptive-power-of-virtual-currency
The disruptive-power-of-virtual-currencyThe disruptive-power-of-virtual-currency
The disruptive-power-of-virtual-currency
Aranca
?
§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç
§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç
§¯§Ñ§Ô§â§Ñ§Õ§í §Ó §Ú§ß§ä§Ö§â§æ§Ö§Û§ã§Ñ§ç
Promodo
?
Beyond Ad-hoc Automation: Leveraging Structured Platforms
Beyond Ad-hoc Automation: Leveraging Structured PlatformsBeyond Ad-hoc Automation: Leveraging Structured Platforms
Beyond Ad-hoc Automation: Leveraging Structured Platforms
bridgetkromhout
?
20 Survival Tips For Virgin Founders
20 Survival Tips For Virgin Founders20 Survival Tips For Virgin Founders
20 Survival Tips For Virgin Founders
Vitaly Golomb
?
MMA F¨®rum Brasil 2015MMA F¨®rum Brasil 2015
MMA F¨®rum Brasil 2015
Mobile Marketing Association
?
Purpose Driven Student Leadership for Civic Engagement
Purpose Driven Student Leadership for Civic EngagementPurpose Driven Student Leadership for Civic Engagement
Purpose Driven Student Leadership for Civic Engagement
Matt Cummings
?
ISBN - Jenn Lim (Rancho Palos Verdes, CA)
ISBN - Jenn Lim (Rancho Palos Verdes, CA)ISBN - Jenn Lim (Rancho Palos Verdes, CA)
ISBN - Jenn Lim (Rancho Palos Verdes, CA)
Delivering Happiness
?
SPT 101 - Office 365 and Hybrid Solutions
SPT 101 - Office 365 and Hybrid SolutionsSPT 101 - Office 365 and Hybrid Solutions
SPT 101 - Office 365 and Hybrid Solutions
Dan Usher
?

Similar to ????? ?????? (20)

Why everyone like ruby
Why everyone like rubyWhy everyone like ruby
Why everyone like ruby
Ivan Grishaev
?
What I Love About Ruby
What I Love About RubyWhat I Love About Ruby
What I Love About Ruby
Keith Bennett
?
Kickin' Ass with Cache-Fu (without notes)
Kickin' Ass with Cache-Fu (without notes)Kickin' Ass with Cache-Fu (without notes)
Kickin' Ass with Cache-Fu (without notes)
err
?
Variables, expressions, standard types
 Variables, expressions, standard types  Variables, expressions, standard types
Variables, expressions, standard types
Rubizza
?
Why learn Internals?
Why learn Internals?Why learn Internals?
Why learn Internals?
Shaul Rosenzwieg
?
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
Michael Pirnat
?
All about Erubis (English)
All about Erubis (English)All about Erubis (English)
All about Erubis (English)
kwatch
?
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
aztack
?
[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...
[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...
[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...
EnlightenmentProject
?
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVM
Charles Nutter
?
Hidden Gems in Swift
Hidden Gems in SwiftHidden Gems in Swift
Hidden Gems in Swift
Netguru
?
Memcached Presentation @757rb
Memcached Presentation @757rbMemcached Presentation @757rb
Memcached Presentation @757rb
Ken Collins
?
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
?
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
MobME Technical
?
Minicurso Ruby e Rails
Minicurso Ruby e RailsMinicurso Ruby e Rails
Minicurso Ruby e Rails
SEA Tecnologia
?
Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09
Michelangelo van Dam
?
DataMapper
DataMapperDataMapper
DataMapper
Yehuda Katz
?
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
Henry S
?
Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant)
Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant) Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant)
Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant)
BigDataEverywhere
?
Ruby and Rails by Example (GeekCamp edition)
Ruby and Rails by Example (GeekCamp edition)Ruby and Rails by Example (GeekCamp edition)
Ruby and Rails by Example (GeekCamp edition)
bryanbibat
?
Kickin' Ass with Cache-Fu (without notes)
Kickin' Ass with Cache-Fu (without notes)Kickin' Ass with Cache-Fu (without notes)
Kickin' Ass with Cache-Fu (without notes)
err
?
Variables, expressions, standard types
 Variables, expressions, standard types  Variables, expressions, standard types
Variables, expressions, standard types
Rubizza
?
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
Michael Pirnat
?
All about Erubis (English)
All about Erubis (English)All about Erubis (English)
All about Erubis (English)
kwatch
?
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
aztack
?
[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...
[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...
[E-Dev-Day 2014][4/16] Review of Eolian, Eo, Bindings, Interfaces and What's ...
EnlightenmentProject
?
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVM
Charles Nutter
?
Hidden Gems in Swift
Hidden Gems in SwiftHidden Gems in Swift
Hidden Gems in Swift
Netguru
?
Memcached Presentation @757rb
Memcached Presentation @757rbMemcached Presentation @757rb
Memcached Presentation @757rb
Ken Collins
?
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
?
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
Henry S
?
Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant)
Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant) Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant)
Big Data Everywhere Chicago: Unleash the Power of HBase Shell (Conversant)
BigDataEverywhere
?
Ruby and Rails by Example (GeekCamp edition)
Ruby and Rails by Example (GeekCamp edition)Ruby and Rails by Example (GeekCamp edition)
Ruby and Rails by Example (GeekCamp edition)
bryanbibat
?

More from mahersaif (15)

?????? ????? ??? ?????
?????? ????? ??? ??????????? ????? ??? ?????
?????? ????? ??? ?????
mahersaif
?
What is the Pre-Approved Program?
What is the Pre-Approved Program?What is the Pre-Approved Program?
What is the Pre-Approved Program?
mahersaif
?
a course
a coursea course
a course
mahersaif
?
The Professional in Human Resources (PHR?) certification is designed for the ...
The Professional in Human Resources (PHR?) certification is designed for the ...The Professional in Human Resources (PHR?) certification is designed for the ...
The Professional in Human Resources (PHR?) certification is designed for the ...
mahersaif
?
??????? ???????
??????? ?????????????? ???????
??????? ???????
mahersaif
?
??? ???? ????????
??? ???? ??????????? ???? ????????
??? ???? ????????
mahersaif
?
??????
????????????
??????
mahersaif
?
Right Compensation Plan
Right Compensation PlanRight Compensation Plan
Right Compensation Plan
mahersaif
?
ruby
rubyruby
ruby
mahersaif
?
ruby
rubyruby
ruby
mahersaif
?
ruby
rubyruby
ruby
mahersaif
?
ruby
rubyruby
ruby
mahersaif
?
I Love Ruby
I Love RubyI Love Ruby
I Love Ruby
mahersaif
?
I Love Ruby
I Love RubyI Love Ruby
I Love Ruby
mahersaif
?
?????? ????? ??? ?????
?????? ????? ??? ??????????? ????? ??? ?????
?????? ????? ??? ?????
mahersaif
?
What is the Pre-Approved Program?
What is the Pre-Approved Program?What is the Pre-Approved Program?
What is the Pre-Approved Program?
mahersaif
?
The Professional in Human Resources (PHR?) certification is designed for the ...
The Professional in Human Resources (PHR?) certification is designed for the ...The Professional in Human Resources (PHR?) certification is designed for the ...
The Professional in Human Resources (PHR?) certification is designed for the ...
mahersaif
?
??????? ???????
??????? ?????????????? ???????
??????? ???????
mahersaif
?
??? ???? ????????
??? ???? ??????????? ???? ????????
??? ???? ????????
mahersaif
?
Right Compensation Plan
Right Compensation PlanRight Compensation Plan
Right Compensation Plan
mahersaif
?

????? ??????

  • 1. Keith Bennett : What I Love About Ruby This page last changed on Sep 17, 2008 by kbennett. What I Love About Ruby Keith Bennett kbennett .at. bbsinc .dot. biz Simplicity of Creating Instance Variables with Accessors and Mutators in Ruby class Y attr_accessor :a end ...creates an instance variable a, and an accessor and mutator. Concise Idiom for Conditional (and Lazy) Initialization @var ||= some_expensive_initialization ...means if var is undefined, define it, and if nil, do the initialization. Numeric Constants Thousands Separators Supported irb(main):002:0> 1_000_000 => 1000000 irb(main):003:0> 1_000_000.class => Fixnum Actually, all underscores are stripped, even if they do not separate thousands. Shell Integration A shell command enclosed in backticks will be run, and the value returned by the backticked command will be the text the command sent to stdout: irb(main):008:0> `mkdir a b c d` => "" irb(main):009:0> `touch b/foo d/foo` => "" irb(main):010:0> emptydirs = `find . -type d -empty` => "./an./cn" irb(main):011:0> puts emptydirs ./a ./c => nil Logical Syntax: 1.upto(10) { |i| puts i } (100..200).each { |n| puts n } Document generated by Confluence on Sep 18, 2008 09:45 Page 1
  • 2. vs., in Java, for the first example: for (int i = 0; i <= 10; i++) { System.out.println(i) ; } Ability to Specify Arrays (and Hashes) as Literals and the Ease of Iterating Over Them irb(main):018:0> ['collie', 'labrador', 'husky'].each { |breed| puts "Hi, I'm a #{breed}, and I know how to bark." } Hi, I'm a collie, and I know how to bark. Hi, I'm a labrador, and I know how to bark. Hi, I'm a husky, and I know how to bark. => ["collie", "labrador", "husky"] Also: %w(collie labrador husky) can be used to create the array instead of: ['collie', 'labrador', 'husky'] A Hash: irb(main):063:0> favorites = { :fruit => :durian, :vegetable => :broccoli } => {:fruit=>:durian, :vegetable=>:broccoli} Ranges water_liquid_range = 32.0...212.0 => 32.0...212.0 irb(main):010:0> water_liquid_range.include? 40 => true irb(main):011:0> water_liquid_range.include? -40 => false Note: Ranges are not arrays; any number n, not just integers, such that 32.0 <= n < 212.0, is included in the range. Converting Ranges to Arrays: irb(main):043:0> ('m'..'q').to_a => ["m", "n", "o", "p", "q"] Blocks Used to Automatically Close Resources File.open 'x.txt', 'w' do |file| file << 'Hello, world' Document generated by Confluence on Sep 18, 2008 09:45 Page 2
  • 3. end The file is automatically closed after the block completes. If no block is provided, then the open function returns the file instance: irb(main):001:0> f = File.open 'x.txt', 'w' => #<File:x.txt> irb(main):002:0> f << "Pleaaase, delete me, let me go..." => #<File:x.txt> irb(main):003:0> f.close => nil irb(main):004:0> puts IO.read('x.txt') Pleaaase, delete me, let me go... => nil Simple File Operations file_as_lines_array = IO.readlines 'x.txt' file_as_single_string = IO.read 'x.txt' Clean and Simple Syntax puts Array.instance_methods.sort Regular Expressions irb(main):027:0> 'ruby' =~ /ruby/ => 0 irb(main):028:0> 'rubx' =~ /ruby/ => nil irb(main):029:0> 'ruby' =~ /Ruby/ => nil irb(main):030:0> 'ruby' =~ /Ruby/i => 0 Arrays: irb(main):001:0> nums = [1,2,3,4,5] => [1, 2, 3, 4, 5] irb(main):006:0> nums.include? 3 => true irb(main):004:0> nums.collect { |n| n * n } => [1, 4, 9, 16, 25] irb(main):002:0> nums.reject { |n| n % 2 == 0} => [1, 3, 5] irb(main):003:0> nums.inject { |sum,n| sum += n } => 15 irb(main):052:0* distances_in_miles = [10, 50]=> [10, 50] irb(main):053:0> distances_in_km = distances_in_miles.map { |n| n * 9.0 / 5.0 } => [18.0, 90.0]irb(main):016:0* twos = (0..10).map { |n| n * 2 } => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20] irb(main):017:0> fours = (0..5).map { |n| n * 4 } => [0, 4, 8, 12, 16, 20] irb(main):018:0> twos - fours => [2, 6, 10, 14, 18] irb(main):019:0> twos & fours Document generated by Confluence on Sep 18, 2008 09:45 Page 3
  • 4. => [0, 4, 8, 12, 16, 20] irb(main):020:0> fours * 2 => [0, 4, 8, 12, 16, 20, 0, 4, 8, 12, 16, 20] irb(main):021:0> twos + fours => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 0, 4, 8, 12, 16, 20] Built-in String Operations ? Case Conversions, Capitalization ? Left, Right, Sub ? Strip, Justify, Center ? Search and Replace (gsub) ? Insert, Delete Document generated by Confluence on Sep 18, 2008 09:45 Page 4