Learn Ruby 2011
     Session 5
Our Sponsors
Flatsourcing, iSeatz, Koda, and Launchpad
Questions
Having any problems? Discover anything cool?
Looking for a Rescue
   Blocks, Iterators & Error Handling
Blocks

• Blocks are chunks of code between
  brackets ( {} ) or begin and end

• Blocks can be passed to methods like a
  parameter
• Blocks can also accept parameters
Blocks

ary = [“Tom”, “Dick”, “Harry”]
ary.each do |name|
  puts name
end

Tom
Dick
Harry
Blocks

ary = [“Tom”, “Dick”, “Harry”]
ary.each { |name| puts name }

Tom
Dick
Harry
Blocks
           class Array
             def iterate!
               self.each_with_index do |n, i|
                 self[i] = yield(n)
               end
             end
           end

           array = [1, 2, 3, 4]

           array.iterate! do |n|
             n ** 2
           end

           puts array.inspect

           # => [1, 4, 9, 16]


http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Procs

• Procs are a special kind of block
• Procs are discreet Objects
• Procs set extra parameters to Nil
Procs
           class Array
             def iterate!(&code)
               self.each_with_index do |n, i|
                 self[i] = code.call(n)
               end
             end
           end

           array = [1, 2, 3, 4]

           array.iterate! do |n|
             n ** 2
           end

           puts array.inspect

           # => [1, 4, 9, 16]


http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Procs
           class Array
             def iterate!(code)
               self.each_with_index do |n, i|
                 self[i] = code.call(n)
               end
             end
           end

           array = [1, 2, 3, 4]

           square = Proc.new do |n|
             n ** 2
           end

           array.iterate!(square)

           puts array.inspect

           # => [1, 4, 9, 16]



http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Lambdas

• Lambdas are another special kind of block
• Unlike Procs, Lambdas check the number of
  arguments passed to them
• Lambdas behave more like methods
Lambdas

           def args(code)
             one, two = 1, 2
             code.call(one, two)
           end

           args(Proc.new{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})

           args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})

           # => Give me a 1 and a 2 and a NilClass
           #*.rb:8: ArgumentError: wrong number of arguments (2 for 3) (ArgumentError)




http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Lambdas
           def proc_return
             Proc.new { return "Proc.new"}.call
             return "proc_return method finished"
           end

           def lambda_return
             lambda { return "lambda" }.call
             return "lambda_return method finished"
           end

           puts proc_return
           puts lambda_return

           # => Proc.new
           # => lambda_return method finished


http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Tell Me About Iterators
Exceptions

• An Exception is an error
• Exceptions are raised when something goes
  wrong
• You can create your own exceptions
Exceptions
def multiply(a, b)
  unless a.is_a?(Fixnum) && b.is_a?(Fixnum)
    raise “Arguments must be Fixnums!”
  end

  puts a * b
end

multiply 6, 2
multiply “F”, 2
multiple 4, true

12
Arguments must be Fixnums!
Arguments must be Fixnums!
Exceptions
def multiply(a, b)
  unless a.is_a?(Fixnum) && b.is_a?(Fixnum)
    raise ArgumentError
  end

  puts a * b
end

multiply 6, 2
multiply “F”, 2
multiple 4, true

12
ArgumentError: ArgumentError
ArgumentError: ArgumentError
Exceptions
def multiply(a, b)
  unless a.is_a?(Fixnum) && b.is_a?(Fixnum)
    raise ArgumentError, “must be Fixnums!”
  end

  puts a * b
end

multiply 6, 2
multiply “F”, 2
multiple 4, true

12
ArgumentError: must be Fixnums!
ArgumentError: must be Fixnums!
Begin...Rescue

• Ruby provides a mechanism for catching
  Exceptions and handling them gracefully.
• Ruby allows you to catch specific Exceptions
  or any Exception that might be raised
Begin...Rescue
def multiply(a, b)
  begin
    puts a * b
  rescue
    puts “Ooops!”
  end
end

multiply 6, 2
multiply “F”, 2
multiple 4, true

12
Ooops!
Ooops!
Begin...Rescue
def multiply(a, b)
  begin
    puts a * b
  rescue TypeError => e
    puts “Ooops! #{e.message}”
  end
end

multiply 6, 2
multiply “F”, 2
multiple 4, true

12
Ooops! String can't be coerced into Fixnum
Ooops! String can't be coerced into Fixnum
Reviewing

• Blocks
• Procs
• Lambdas
• Exceptions
• Begin...Rescue
For Next Week
For the New to Programming

•   Read Chapters 14 & 15 in LtP

•   Complete exercises for each chapter
For Everyone

•   Read Chapter 4, 8, 10 in PR1.9

•   Read more about blocks at http://www.robertsosinski.com/2008/12/21/
    understanding-ruby-blocks-procs-and-lambdas/


•   Work through Ruby Koans: about_blocks and
    about_iteration
Next Week


• Review
• The Ruby Community
Exercises

• Work on specific Ruby Koans:
 • ruby about_blocks.rb
 • ruby about_iteration.rb

Learn Ruby 2011 - Session 5 - Looking for a Rescue

  • 1.
  • 2.
  • 3.
    Questions Having any problems?Discover anything cool?
  • 4.
    Looking for aRescue Blocks, Iterators & Error Handling
  • 5.
    Blocks • Blocks arechunks of code between brackets ( {} ) or begin and end • Blocks can be passed to methods like a parameter • Blocks can also accept parameters
  • 6.
    Blocks ary = [“Tom”,“Dick”, “Harry”] ary.each do |name| puts name end Tom Dick Harry
  • 7.
    Blocks ary = [“Tom”,“Dick”, “Harry”] ary.each { |name| puts name } Tom Dick Harry
  • 8.
    Blocks class Array def iterate! self.each_with_index do |n, i| self[i] = yield(n) end end end array = [1, 2, 3, 4] array.iterate! do |n| n ** 2 end puts array.inspect # => [1, 4, 9, 16] http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
  • 9.
    Procs • Procs area special kind of block • Procs are discreet Objects • Procs set extra parameters to Nil
  • 10.
    Procs class Array def iterate!(&code) self.each_with_index do |n, i| self[i] = code.call(n) end end end array = [1, 2, 3, 4] array.iterate! do |n| n ** 2 end puts array.inspect # => [1, 4, 9, 16] http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
  • 11.
    Procs class Array def iterate!(code) self.each_with_index do |n, i| self[i] = code.call(n) end end end array = [1, 2, 3, 4] square = Proc.new do |n| n ** 2 end array.iterate!(square) puts array.inspect # => [1, 4, 9, 16] http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
  • 12.
    Lambdas • Lambdas areanother special kind of block • Unlike Procs, Lambdas check the number of arguments passed to them • Lambdas behave more like methods
  • 13.
    Lambdas def args(code) one, two = 1, 2 code.call(one, two) end args(Proc.new{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"}) args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"}) # => Give me a 1 and a 2 and a NilClass #*.rb:8: ArgumentError: wrong number of arguments (2 for 3) (ArgumentError) http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
  • 14.
    Lambdas def proc_return Proc.new { return "Proc.new"}.call return "proc_return method finished" end def lambda_return lambda { return "lambda" }.call return "lambda_return method finished" end puts proc_return puts lambda_return # => Proc.new # => lambda_return method finished http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
  • 15.
    Tell Me AboutIterators
  • 16.
    Exceptions • An Exceptionis an error • Exceptions are raised when something goes wrong • You can create your own exceptions
  • 17.
    Exceptions def multiply(a, b) unless a.is_a?(Fixnum) && b.is_a?(Fixnum) raise “Arguments must be Fixnums!” end puts a * b end multiply 6, 2 multiply “F”, 2 multiple 4, true 12 Arguments must be Fixnums! Arguments must be Fixnums!
  • 18.
    Exceptions def multiply(a, b) unless a.is_a?(Fixnum) && b.is_a?(Fixnum) raise ArgumentError end puts a * b end multiply 6, 2 multiply “F”, 2 multiple 4, true 12 ArgumentError: ArgumentError ArgumentError: ArgumentError
  • 19.
    Exceptions def multiply(a, b) unless a.is_a?(Fixnum) && b.is_a?(Fixnum) raise ArgumentError, “must be Fixnums!” end puts a * b end multiply 6, 2 multiply “F”, 2 multiple 4, true 12 ArgumentError: must be Fixnums! ArgumentError: must be Fixnums!
  • 20.
    Begin...Rescue • Ruby providesa mechanism for catching Exceptions and handling them gracefully. • Ruby allows you to catch specific Exceptions or any Exception that might be raised
  • 21.
    Begin...Rescue def multiply(a, b) begin puts a * b rescue puts “Ooops!” end end multiply 6, 2 multiply “F”, 2 multiple 4, true 12 Ooops! Ooops!
  • 22.
    Begin...Rescue def multiply(a, b) begin puts a * b rescue TypeError => e puts “Ooops! #{e.message}” end end multiply 6, 2 multiply “F”, 2 multiple 4, true 12 Ooops! String can't be coerced into Fixnum Ooops! String can't be coerced into Fixnum
  • 23.
    Reviewing • Blocks • Procs •Lambdas • Exceptions • Begin...Rescue
  • 24.
    For Next Week Forthe New to Programming • Read Chapters 14 & 15 in LtP • Complete exercises for each chapter For Everyone • Read Chapter 4, 8, 10 in PR1.9 • Read more about blocks at http://www.robertsosinski.com/2008/12/21/ understanding-ruby-blocks-procs-and-lambdas/ • Work through Ruby Koans: about_blocks and about_iteration
  • 25.
    Next Week • Review •The Ruby Community
  • 26.
    Exercises • Work onspecific Ruby Koans: • ruby about_blocks.rb • ruby about_iteration.rb