Successfully reported this slideshow.
Your SlideShare is downloading. ×

Ruby : Block, Proc and, lambda

Ad

Block, Proc and, lambda




                          2

Ad

Matthieu Segret
Formateur Ruby on Rails @human_coders




                                        1

Ad

Block




        3

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Upcoming SlideShare
Rust言語紹介
Rust言語紹介
Loading in …3
×

Check these out next

1 of 24 Ad
1 of 24 Ad

Ruby : Block, Proc and, lambda

Download to read offline

En tant que formateur Ruby, je rencontre régulièrement des développeurs manipulant les block, Proc et lambda sans toujours comprendre comment cela fonctionne. J'ai donc eu l'idée de faire cette présentation au Meetup Paris.rb. (10 décembre 2012)

En tant que formateur Ruby, je rencontre régulièrement des développeurs manipulant les block, Proc et lambda sans toujours comprendre comment cela fonctionne. J'ai donc eu l'idée de faire cette présentation au Meetup Paris.rb. (10 décembre 2012)

Advertisement
Advertisement

More Related Content

Advertisement

Ruby : Block, Proc and, lambda

  1. 1. Block, Proc and, lambda 2
  2. 2. Matthieu Segret Formateur Ruby on Rails @human_coders 1
  3. 3. Block 3
  4. 4. Block names = ["Matz","DHH","Aaron"] names.each do |name| puts name end names.each { |name| puts name } 4
  5. 5. yield def call_this_block_twice yield yield end Matz call_this_block_twice { puts "Matz" } Matz 5
  6. 6. yield - Arguments def call_this_block yield("Matz") end call_this_block {|name| puts name} Matz 6
  7. 7. yield - Return value def puts_this_block block_result = yield puts block_result end puts_this_block {"Matz"} Matz 7
  8. 8. yield - Optional block def call_this_block yield("Matz") end call_this_block no block given 8
  9. 9. yield - Optional block def call_this_block yield("Matz") if block_given? end call_this_block 9
  10. 10. yield - Scope 1 def call_this_block yield end x = 5 puts "value of x before: #{x}" value of x before: 5 call_this_block { x += 1 } puts "value of x after: #{x}" value of x after: 6 10
  11. 11. yield - Scope 2 def call_this_block x = 100 yield puts "value of x at end of call_this_block_x: #{x}" end x = 5 value of x at end of call_this_block { x += 1 } call_this_block_x: 100 puts "value of x after: #{x}" value of x after: 6 11
  12. 12. What if we wanted to store this block, for execution later? {|name| puts name} 12
  13. 13. Proc & lambda 13
  14. 14. Proc & lambda Proc my_proc = Proc.new {|x| puts x} my_proc.call("Matz") Matz Lambda my_lambda = lambda {|x| puts x} my_lambda.call("DHH") DHH Lambda Ruby 1.9 my_lambda = ->(x) {puts x} my_lambda.call("Aaron") Aaron 14
  15. 15. Proc vs lambda : return Lambda def bar f = lambda { return "return from inside lambda" } f.call return "return from bar" end puts bar return from bar Proc def foo f = Proc.new { return "return from inside proc" } f.call return "return from foo" end puts foo return from inside proc 15
  16. 16. Proc vs lambda : arity Lambda my_lambda = lambda {|x,y| puts "(#{x},#{y})"} my_lambda.call(1,2,3) wrong number of my_lambda.call(1) arguments ... Proc my_proc = Proc.new {|x,y| puts "(#{x},#{y})"} my_proc.call(1,2,3) (1,2) my_proc.call(1) (1,) 16
  17. 17. Multiple lambda def post(success, error) ... if ... success.call else error.call end end post(-> {puts "Sent!"}, -> {raise 'Auth Error'}) 17
  18. 18. Lambda to block names = ["Matz","DHH","Aaron"] names.each do |name| puts name end names = ["Matz","DHH","Aaron"] printer = lambda {|name| puts name} names.each( & printer) lambda to block 18
  19. 19. Proc to block names = ["Matz","DHH","Aaron"] names.each do |name| puts name end names = ["Matz","DHH","Aaron"] printer = Proc.new {|name| puts name} names.each( & printer) Proc to block 19
  20. 20. Block to Proc def call_this_block yield("Matz") end def call_this_block(&block) block to Proc block.call("Matz") end 20
  21. 21. Symbol#to_proc tweets.map { |tweet| tweet.user } tweets.map(&:user) Symbol to Proc 21
  22. 22. Method#to_proc def method_user(tweet) tweet.user end method(:method_user) tweets.map(&method(:method_user)) Method to Proc 22
  23. 23. 23
  24. 24. Thanks @matthieusegret 24

×