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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Learn Ruby 2011 - Session 5 - Looking for a Rescue

801

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
801
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

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
  • \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
  • Transcript

    • 1. Learn Ruby 2011 Session 5
    • 2. Our SponsorsFlatsourcing, iSeatz, Koda, and Launchpad
    • 3. QuestionsHaving any problems? Discover anything cool?
    • 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. Blocksary = [“Tom”, “Dick”, “Harry”]ary.each do |name| puts nameendTomDickHarry
    • 7. Blocksary = [“Tom”, “Dick”, “Harry”]ary.each { |name| puts name }TomDickHarry
    • 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/
    • 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 finishedhttp://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
    • 15. Tell Me About Iterators
    • 16. Exceptions• An Exception is an error• Exceptions are raised when something goes wrong• You can create your own exceptions
    • 17. 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!
    • 18. 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
    • 19. 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!
    • 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...Rescuedef multiply(a, b) begin puts a * b rescue puts “Ooops!” endendmultiply 6, 2multiply “F”, 2multiple 4, true12Ooops!Ooops!
    • 22. 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
    • 23. Reviewing• Blocks• Procs• Lambdas• Exceptions• Begin...Rescue
    • 24. 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
    • 25. Next Week• Review• The Ruby Community
    • 26. Exercises• Work on specific Ruby Koans: • ruby about_blocks.rb • ruby about_iteration.rb

    ×