+ All Categories
Transcript
Page 1: Ruby : Block, Proc and, lambda

Block, Proc and, lambda

2

Page 2: Ruby : Block, Proc and, lambda

Formateur Ruby on Rails @human_coders

Matthieu Segret

1

Page 3: Ruby : Block, Proc and, lambda

Block

3

Page 4: Ruby : Block, Proc and, lambda

Block

names.each do |name| puts name end

names.each { |name| puts name }

names = ["Matz","DHH","Aaron"]

4

Page 5: Ruby : Block, Proc and, lambda

yield

def call_this_block_twice yield yieldend

MatzMatzcall_this_block_twice { puts "Matz" }

5

Page 6: Ruby : Block, Proc and, lambda

def call_this_block yield("Matz")end

Matz

yield - Arguments

call_this_block {|name| puts name}

6

Page 7: Ruby : Block, Proc and, lambda

yield - Return value

def puts_this_block block_result = yield puts block_resultend

puts_this_block {"Matz"} Matz

7

Page 8: Ruby : Block, Proc and, lambda

def call_this_block yield("Matz")end

no block given

yield - Optional block

call_this_block

8

Page 9: Ruby : Block, Proc and, lambda

def call_this_block yield("Matz") if block_given?end

yield - Optional block

call_this_block

9

Page 10: Ruby : Block, Proc and, lambda

yield - Scope 1

def call_this_block yieldend

x = 5puts "value of x before: #{x}"call_this_block { x += 1 }puts "value of x after: #{x}" value of x after: 6

value of x before: 5

10

Page 11: Ruby : Block, Proc and, lambda

yield - Scope 2

def call_this_block x = 100 yield puts "value of x at end of call_this_block_x: #{x}"end

x = 5call_this_block { x += 1 }

puts "value of x after: #{x}" value of x after: 6

value of x at end of call_this_block_x: 100

11

Page 12: Ruby : Block, Proc and, lambda

{|name| puts name}

What if we wanted to store this block, for execution later?

12

Page 13: Ruby : Block, Proc and, lambda

Proc & lambda

13

Page 14: Ruby : Block, Proc and, lambda

Proc & lambda

my_proc = Proc.new {|x| puts x}my_proc.call("Matz")

Proc

Matz

my_lambda = lambda {|x| puts x}my_lambda.call("DHH")

Lambda

DHH

my_lambda = ->(x) {puts x}my_lambda.call("Aaron")

Lambda Ruby 1.9

Aaron

14

Page 15: Ruby : Block, Proc and, lambda

def bar f = lambda { return "return from inside lambda" } f.call return "return from bar" endputs bar

Lambda

def foo f = Proc.new { return "return from inside proc" } f.call return "return from foo" endputs foo

Proc

Proc vs lambda : return

return from bar

return from inside proc

15

Page 16: Ruby : Block, Proc and, lambda

Proc vs lambda : arity

my_proc = Proc.new {|x,y| puts "(#{x},#{y})"}my_proc.call(1,2,3)my_proc.call(1)

Proc

(1,2)(1,)

my_lambda = lambda {|x,y| puts "(#{x},#{y})"}my_lambda.call(1,2,3)my_lambda.call(1)

Lambda

wrong number of arguments ...

16

Page 17: Ruby : Block, Proc and, lambda

Multiple lambda

def post(success, error) ... if ... success.call else error.call end end

post(-> {puts "Sent!"}, -> {raise 'Auth Error'})

17

Page 18: Ruby : Block, Proc and, lambda

Lambda to block

names = ["Matz","DHH","Aaron"]names.each do |name| puts nameend

names = ["Matz","DHH","Aaron"]printer = lambda {|name| puts name}names.each( printer)& lambda to block

18

Page 19: Ruby : Block, Proc and, lambda

Proc to block

names = ["Matz","DHH","Aaron"]names.each do |name| puts nameend

names = ["Matz","DHH","Aaron"]printer = Proc.new {|name| puts name}names.each( printer)& Proc to block

19

Page 20: Ruby : Block, Proc and, lambda

Block to Proc

def call_this_block yield("Matz")end

def call_this_block(&block) block.call("Matz")end

block to Proc

20

Page 21: Ruby : Block, Proc and, lambda

Symbol#to_proc

tweets.map { |tweet| tweet.user }

tweets.map(&:user) Symbol to Proc

21

Page 22: Ruby : Block, Proc and, lambda

Method#to_proc

def method_user(tweet) tweet.userend

tweets.map(&method(:method_user)) Method to Proc

method(:method_user)

22

Page 23: Ruby : Block, Proc and, lambda

23

Page 24: Ruby : Block, Proc and, lambda

Thanks@matthieusegret

24


Top Related