In this final language-focussed session we covered the three kinds of blocks used in Ruby: blocks, Procs and lambdas. We also covered error/exception handling in Ruby and reviewed iteration mechanisms.
4. Looking for a Rescue
Blocks, Iterators & Error Handling
5. 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
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 are a 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 are another 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/
16. Exceptions
• An Exception is 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 provides a 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
24. 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