Learn Ruby 2011 - Session 5 - Looking for a Rescue
Upcoming SlideShare
Loading in...5
×
 

Learn Ruby 2011 - Session 5 - Looking for a Rescue

on

  • 992 views

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 ...

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.

Statistics

Views

Total Views
992
Views on SlideShare
992
Embed Views
0

Actions

Likes
1
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Learn Ruby 2011 - Session 5 - Looking for a Rescue Learn Ruby 2011 - Session 5 - Looking for a Rescue Presentation Transcript

  • Learn Ruby 2011 Session 5
  • Our SponsorsFlatsourcing, iSeatz, Koda, and Launchpad
  • QuestionsHaving 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
  • Blocksary = [“Tom”, “Dick”, “Harry”]ary.each do |name| puts nameendTomDickHarry
  • Blocksary = [“Tom”, “Dick”, “Harry”]ary.each { |name| puts name }TomDickHarry
  • 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 finishedhttp://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
  • Exceptionsdef multiply(a, b) unless a.is_a?(Fixnum) && b.is_a?(Fixnum) raise “Arguments must be Fixnums!” end puts a * bendmultiply 6, 2multiply “F”, 2multiple 4, true12Arguments must be Fixnums!Arguments must be Fixnums!
  • Exceptionsdef multiply(a, b) unless a.is_a?(Fixnum) && b.is_a?(Fixnum) raise ArgumentError end puts a * bendmultiply 6, 2multiply “F”, 2multiple 4, true12ArgumentError: ArgumentErrorArgumentError: ArgumentError
  • Exceptionsdef multiply(a, b) unless a.is_a?(Fixnum) && b.is_a?(Fixnum) raise ArgumentError, “must be Fixnums!” end puts a * bendmultiply 6, 2multiply “F”, 2multiple 4, true12ArgumentError: 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...Rescuedef multiply(a, b) begin puts a * b rescue puts “Ooops!” endendmultiply 6, 2multiply “F”, 2multiple 4, true12Ooops!Ooops!
  • Begin...Rescuedef multiply(a, b) begin puts a * b rescue TypeError => e puts “Ooops! #{e.message}” endendmultiply 6, 2multiply “F”, 2multiple 4, true12Ooops! String cant be coerced into FixnumOoops! String cant be coerced into Fixnum
  • Reviewing• Blocks• Procs• Lambdas• Exceptions• Begin...Rescue
  • For Next WeekFor the New to Programming• Read Chapters 14 & 15 in LtP• Complete exercises for each chapterFor 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