Upcoming SlideShare
×

# Ruby : Block, Proc and, lambda

4,898
-1

Published on

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)

Published in: Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
4,898
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
16
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Ruby : Block, Proc and, lambda

1. 1. Block, Proc and, lambda 2
2. 2. Matthieu SegretFormateur Ruby on Rails @human_coders 1
3. 3. Block 3
4. 4. Blocknames = ["Matz","DHH","Aaron"]names.each do |name| puts nameendnames.each { |name| puts name } 4
5. 5. yielddef call_this_block_twice yield yieldend Matzcall_this_block_twice { puts "Matz" } Matz 5
6. 6. yield - Argumentsdef call_this_block yield("Matz")endcall_this_block {|name| puts name} Matz 6
7. 7. yield - Return valuedef puts_this_block block_result = yield puts block_resultendputs_this_block {"Matz"} Matz 7
8. 8. yield - Optional blockdef call_this_block yield("Matz")endcall_this_block no block given 8
9. 9. yield - Optional blockdef call_this_block yield("Matz") if block_given?endcall_this_block 9
10. 10. yield - Scope 1def call_this_block yieldendx = 5puts "value of x before: #{x}" value of x before: 5call_this_block { x += 1 }puts "value of x after: #{x}" value of x after: 6 10
11. 11. yield - Scope 2def call_this_block x = 100 yield puts "value of x at end of call_this_block_x: #{x}"endx = 5 value of x at end ofcall_this_block { x += 1 } call_this_block_x: 100puts "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 Procmy_proc = Proc.new {|x| puts x}my_proc.call("Matz") Matz Lambdamy_lambda = lambda {|x| puts x}my_lambda.call("DHH") DHH Lambda Ruby 1.9my_lambda = ->(x) {puts x}my_lambda.call("Aaron") Aaron 14
15. 15. Proc vs lambda : return Lambdadef bar f = lambda { return "return from inside lambda" } f.call return "return from bar"endputs bar return from bar Procdef foo f = Proc.new { return "return from inside proc" } f.call return "return from foo"endputs foo return from inside proc 15
16. 16. Proc vs lambda : arity Lambdamy_lambda = lambda {|x,y| puts "(#{x},#{y})"}my_lambda.call(1,2,3) wrong number ofmy_lambda.call(1) arguments ... Procmy_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 lambdadef post(success, error) ... if ... success.call else error.call endendpost(-> {puts "Sent!"}, -> {raise Auth Error}) 17
18. 18. Lambda to blocknames = ["Matz","DHH","Aaron"]names.each do |name| puts nameendnames = ["Matz","DHH","Aaron"]printer = lambda {|name| puts name}names.each( & printer) lambda to block 18
19. 19. Proc to blocknames = ["Matz","DHH","Aaron"]names.each do |name| puts nameendnames = ["Matz","DHH","Aaron"]printer = Proc.new {|name| puts name}names.each( & printer) Proc to block 19
20. 20. Block to Procdef call_this_block yield("Matz")enddef call_this_block(&block) block to Proc block.call("Matz")end 20
21. 21. Symbol#to_proctweets.map { |tweet| tweet.user }tweets.map(&:user) Symbol to Proc 21
22. 22. Method#to_procdef method_user(tweet) tweet.userendmethod(:method_user)tweets.map(&method(:method_user)) Method to Proc 22
23. 23. 23
24. 24. Thanks@matthieusegret 24
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.