Ruby : Block, Proc and, lambda
Upcoming SlideShare
Loading in...5
×
 

Ruby : Block, Proc and, lambda

on

  • 4,351 views

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

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)

Statistics

Views

Total Views
4,351
Views on SlideShare
2,133
Embed Views
2,218

Actions

Likes
2
Downloads
13
Comments
0

3 Embeds 2,218

http://matthieusegret.com 1772
http://news.humancoders.com 443
http://www.twylah.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

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

Ruby : Block, Proc and, lambda Ruby : Block, Proc and, lambda Presentation Transcript

  • Block, Proc and, lambda 2
  • Matthieu SegretFormateur Ruby on Rails @human_coders 1
  • Block 3
  • Blocknames = ["Matz","DHH","Aaron"]names.each do |name| puts nameendnames.each { |name| puts name } 4
  • yielddef call_this_block_twice yield yieldend Matzcall_this_block_twice { puts "Matz" } Matz 5
  • yield - Argumentsdef call_this_block yield("Matz")endcall_this_block {|name| puts name} Matz 6
  • yield - Return valuedef puts_this_block block_result = yield puts block_resultendputs_this_block {"Matz"} Matz 7
  • yield - Optional blockdef call_this_block yield("Matz")endcall_this_block no block given 8
  • yield - Optional blockdef call_this_block yield("Matz") if block_given?endcall_this_block 9
  • 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
  • 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
  • What if we wanted to store this block, for execution later? {|name| puts name} 12
  • Proc & lambda 13
  • 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
  • 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
  • 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
  • Multiple lambdadef post(success, error) ... if ... success.call else error.call endendpost(-> {puts "Sent!"}, -> {raise Auth Error}) 17
  • 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
  • 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
  • Block to Procdef call_this_block yield("Matz")enddef call_this_block(&block) block to Proc block.call("Matz")end 20
  • Symbol#to_proctweets.map { |tweet| tweet.user }tweets.map(&:user) Symbol to Proc 21
  • Method#to_procdef method_user(tweet) tweet.userendmethod(:method_user)tweets.map(&method(:method_user)) Method to Proc 22
  • 23
  • Thanks@matthieusegret 24