اليوم السعيد
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
281
On Slideshare
279
From Embeds
2
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 2

http://localhost 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Keith Bennett : What I Love About RubyThis page last changed on Sep 17, 2008 by kbennett.What I Love About RubyKeith Bennettkbennett .at. bbsinc .dot. bizSimplicity of Creating Instance Variables with Accessors and Mutators in Rubyclass Y attr_accessor :aend...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 Supportedirb(main):002:0> 1_000_000=> 1000000irb(main):003:0> 1_000_000.class=> FixnumActually, all underscores are stripped, even if they do not separate thousands.Shell IntegrationA shell command enclosed in backticks will be run, and the value returned by the backticked commandwill 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=> nilLogical 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 Literalsand the Ease of Iterating Over Themirb(main):018:0> [collie, labrador, husky].each { |breed| puts "Hi, Im a #{breed}, and I know how to bark."}Hi, Im a collie, and I know how to bark.Hi, Im a labrador, and I know how to bark.Hi, Im 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}Rangeswater_liquid_range = 32.0...212.0=> 32.0...212.0irb(main):010:0> water_liquid_range.include? 40=> trueirb(main):011:0> water_liquid_range.include? -40=> falseNote: Ranges are not arrays; any number n, not just integers, such that 32.0 <= n < 212.0, is includedin the range.Converting Ranges to Arrays:irb(main):043:0> (m..q).to_a=> ["m", "n", "o", "p", "q"]Blocks Used to Automatically Close ResourcesFile.open x.txt, w do |file| file << Hello, worldDocument generated by Confluence on Sep 18, 2008 09:45 Page 2
  • 3. endThe file is automatically closed after the block completes. If no block is provided, then the open functionreturns 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=> nilirb(main):004:0> puts IO.read(x.txt)Pleaaase, delete me, let me go...=> nilSimple File Operationsfile_as_lines_array = IO.readlines x.txtfile_as_single_string = IO.read x.txtClean and Simple Syntaxputs Array.instance_methods.sortRegular Expressionsirb(main):027:0> ruby =~ /ruby/=> 0irb(main):028:0> rubx =~ /ruby/=> nilirb(main):029:0> ruby =~ /Ruby/=> nilirb(main):030:0> ruby =~ /Ruby/i=> 0Arrays:irb(main):001:0> nums = [1,2,3,4,5]=> [1, 2, 3, 4, 5]irb(main):006:0> nums.include? 3=> trueirb(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 }=> 15irb(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 & foursDocument 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, DeleteDocument generated by Confluence on Sep 18, 2008 09:45 Page 4