1 / 10

Learning Ruby - 12

Grabbag II - The Grabbag Strikes Back!. Learning Ruby - 12. Testing Code in irb. load "~/ruby/pcap/Ethernet.rb" packet = Ethernet.new( "77777733333399Some Data" ) packet.dest packet.src packet.type_len packet.payload packet.inspect packet.methods.sort. Less evenmoreless.rb.

jules
Download Presentation

Learning Ruby - 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Grabbag II - The Grabbag Strikes Back! Learning Ruby - 12

  2. Testing Code in irb load "~/ruby/pcap/Ethernet.rb" packet = Ethernet.new( "77777733333399Some Data" ) packet.dest packet.src packet.type_len packet.payload packet.inspect packet.methods.sort

  3. Less evenmoreless.rb class Person attr_reader :name, :dob, :nationality attr_writer :name, :dob, :nationality ... class Person attr_accessor :name, :dob, :nationality ...

  4. Super Inheritance! class RockSong < Song def initialize( name, artist, solo, length ) super( name, artist ) # Initialise the parent. @solo = solo @length = length end # of initialize. end # of RockSong.

  5. Don't addemall, inject 'em! def addemall( first, *rest ) rest.each { |r| first = first + r } first end # of addemall. a = addemall( 1, 2, 3, 4, 5 ) b = [1, 2, 3, 4, 5].inject(0) { |sum, num| sum + num }

  6. Named Arguments def find_person( params ) name = params[:name] dob = params[:dob] || "unknown" # Do yer findin' ... then ... puts name + ', born: ' + dob + '.' end # of find_person. find_person( :name => 'John Doe', :dob => '24/03/1961' ) find_person( :dob => '12/12/1912', :name => 'Jane Jones' ) find_person( :name => 'Harry Holt' )

  7. ++ and -- • The 'familiar' ++ and -- operators are NOT in Ruby • When you want to add 1 to a, use a += 1 • When you want to take 1 from b, use b -= 1 • Boo! Hoo! There's no autoincrement and autodecrement in Ruby!

  8. Things to Remember! • Variables hold references to objects, not the objects themselves! • Variables start with a lowercase letter, Classes and Constants start with an UPPERCASE letter • Writing code that depends on the existence of the default variable ($_) is frowned upon • Qualifying a loop or condition (to quote The PickAxe) is a path that "leads to the gates of madness"

  9. The Gates of Madness!!! i = 10 while i < 15 puts i i += 1 end unless i == 11 name = 'Paul' if name == "Paul" puts name end unless name == "Paul"

  10. More ... Ruby So Far • Be really sure to ignore the code on that last slide!!!!!!!

More Related