142
Blocks, Procs and Lambdas

Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Blocks,Procs and

Lambdas

Page 2: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Block

Page 3: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

A Portable Chunk of Ruby Code

Page 4: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Passing a Block

Page 5: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello"

Page 6: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 7: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 8: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do x = rand(1..10) puts "big!" if x > 5 "yolo #{x}" end

Page 9: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 10: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" { puts "World" }

Page 11: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" { puts "I'm in a block" }

Page 12: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" { puts "World" }

Page 13: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 14: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 15: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 16: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello"

Page 17: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet("Hello")

Page 18: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet("Hello", do puts "World" end)

Page 19: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet("Hello") do puts "World" end

Page 20: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 21: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Receiving a Block

Page 22: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) puts msg

end

Page 23: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) puts msg yield if block_given? end

Page 24: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) puts msg yield if block_given? end

Page 25: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) puts msg yield if block_given? end

Page 26: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 27: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end # Hello # World

Page 28: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) puts msg yield if block_given? yield if block_given? end

Page 29: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end # Hello # World # World

Page 30: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 31: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do "Ruby meetup!" end

Page 32: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello") do "Ruby meetup" end # "Hello, Ruby meetup!"

Page 33: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello") do "Ruby meetup" end # "Hello, Ruby meetup!"

Page 34: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello") do "Ruby meetup" end # "Hello, Ruby meetup!"

Page 35: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello") do "Ruby meetup" end # "Hello, Ruby meetup!"

Page 36: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello")

# "Hello!"

Page 37: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello")

# "Hello!"

Page 38: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello")

# "Hello!"

Page 39: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield Location.city puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello") do |city| "Ruby #{city}" end # "Hello, Ruby Sydney!"

Page 40: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield Location.city puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello") do |city| "Ruby #{city}" end # "Hello, Ruby Sydney!"

Page 41: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield Location.city puts "#{msg}, #{who}!" else puts "#{msg}!" end end

greet("Hello") do |city| "Ruby #{city}" end # "Hello, Ruby Sydney!"

Page 42: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) # ... end

greet("Hello") do |city| "Ruby #{city}" end # "Hello, Ruby Sydney!"

Page 43: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) # ... end

language = "Ruby"

greet("Hello") do |city| "#{language} #{city}" end # "Hello, Ruby Sydney!"

Page 44: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) # ... end

language = "Ruby"

greet("Hello") do |city| "#{language} #{city}" end # "Hello, Ruby Sydney!"

Page 45: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

This All Looks a Little Bit Familiar

Page 46: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 47: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

[1,2,3].each do |x| puts "Item: #{x}" end

Page 48: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

[1,2,3].each do |x| puts "Item: #{x}" end

Page 49: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

[1,2,3].each() do |x| puts "Item: #{x}" end

Page 50: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

[1,2,3].each do |x| puts "Item: #{x}" end

Page 51: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

[1,2,3].each do |x| puts "Item: #{x}" end

Page 52: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

[1,2,3].each do |x| puts "Item: #{x}" end

Page 53: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

[1,2,3].each do |x| puts "Item: #{x}" end

Page 54: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do puts "World" end

Page 55: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Array # ... def each for item in self do yield item if block_given? end end # ... end

Page 56: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Array # ... def each for item in self do yield item if block_given? end end # ... end

Page 57: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Array # ... def each for item in self do yield item if block_given? end end # ... end

Page 58: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Array # ... def each for item in self do yield item if block_given? end end # ... end

Page 59: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Array # ... def each for item in self do yield item if block_given? end end # ... end

Page 60: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Array Enum # ... def each for item in @value do yield item if block_given? end end # ... end

Page 61: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Lambda

Page 62: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

Page 63: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

Page 64: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

Page 65: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

Page 66: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call(2) # 3

Page 67: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call(2) # 3

Page 68: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.(2) # 3

Page 69: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder[2] # 3

Page 70: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call(2) # 3

Page 71: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call(2) # 3

Page 72: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = -> (x) { x + 1 }

puts adder.call(2) # 3

Page 73: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call(2) # 3

Page 74: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call(2) # 3

Page 75: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Adder def call(x) x + 1 end end adder = Adder.new puts adder.call(2) # 3

Page 76: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Proc

Page 77: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

Page 78: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

Page 79: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

Page 80: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Block Position 🏎

Page 81: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

greet "Hello" do |city| "Ruby #{city}" end

Page 82: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

ruby = proc do |city| "Ruby #{city}" end

greet "Hello", &ruby

Page 83: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

ruby = proc do |city| "Ruby #{city}" end

greet "Hello", &ruby

Page 84: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

ruby = proc do |city| "Ruby #{city}" end

greet "Hello", &ruby

Page 85: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

ruby = proc do |city| "Ruby #{city}" end

greet "Hello", &ruby

Page 86: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

ruby = lambda do |city| "Ruby #{city}" end

greet "Hello", &ruby

Page 87: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

ruby = proc do |city| "Ruby #{city}" end

greet "Hello", &ruby

Page 88: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield Location.city puts "#{msg}, #{who}!" else puts "#{msg}!" end end

Page 89: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg) if block_given? who = yield Location.city puts "#{msg}, #{who}!" else puts "#{msg}!" end end

Page 90: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def greet(msg, &block) if block who = block.call Location.city puts "#{msg}, #{who}!" else puts "#{msg}!" end end

Page 91: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

#JustProcThings

Page 92: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call # ArgumentError: wrong number of # arguments (given 0, expected 1)

Page 93: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

puts adder.call # NoMethodError: undefined method # `+' for nil:NilClass

Page 94: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

puts adder.call # NoMethodError: undefined method # `+' for nil:NilClass

Page 95: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

puts adder.call # NoMethodError: undefined method # `+' for nil:NilClass

Page 96: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = lambda do |x| x + 1 end

puts adder.call(2,3) # ArgumentError: wrong number of # arguments (given 2, expected 1)

Page 97: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

puts adder.call(2,3) # 3

Page 98: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

puts adder.call(2,3) # 3

Page 99: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

adder = proc do |x| x + 1 end

puts adder.call(2,3) # 3

Page 100: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

AND

Page 101: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" adder = lambda do |x| return x + 1 puts "ignored" end puts adder.call(2) puts "after" end

Page 102: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" adder = lambda do |x| return x + 1 puts "ignored" end puts adder.call(2) puts "after" end

Page 103: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" # before adder = lambda do |x| return x + 1 puts "ignored" end puts adder.call(2) # 3 puts "after" # after end

Page 104: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" adder = proc do |x| return x + 1 puts "ignored" end puts adder.call(2) puts "after" end

Page 105: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" # before adder = proc do |x| return x + 1 puts "ignored" end puts adder.call(2) # 3 puts "after" end

Page 106: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" # before adder = proc do |x| return x + 1 puts "ignored" end puts adder.call(2) # 3 puts "after" # ...? end

Page 107: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" # before adder = proc do |x| return x + 1 puts "ignored" end puts adder.call(2) # 3 puts "after" # ...? end

Page 108: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" # before adder = proc do |x| return x + 1 puts "ignored" end puts adder.call(2) # 3 puts "after" # ...? end

Page 109: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" # before adder = proc do |x| next x + 1 puts "ignored" end puts adder.call(2) # 3 puts "after" # after end

Page 110: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" [1].each do |x| return x + 1 puts "ignored" end

puts "after" end

Page 111: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" [1].each do |x| return x + 1 puts "ignored" end

puts "after" end

Page 112: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

waitaminute

Page 113: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" # before adder = proc do |x| return x + 1 puts "ignored" end puts adder.call(2) # 3 puts "after" # ...? end

Page 114: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

def example puts "before" [1].each do |x| return x + 1 puts "ignored" end

puts "after" end

Page 115: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Blocks are given to methods as

Procs

Page 116: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

> p_rock = proc { } => #<Proc:…@(irb):1>

Page 117: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

> p_rock = proc { } => #<Proc:…@(irb):1>

> lamb_derr = lambda { } => #<Proc:…@(irb):2 (lambda)>

Page 118: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

> p_rock = proc { } => #<Proc:…@(irb):1>

> lamb_derr = lambda { } => #<Proc:…@(irb):2 (lambda)>

Page 119: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

> p_rock = proc { } => #<Proc:…@(irb):1>

> lamb_derr = lambda { } => #<Proc:…@(irb):2 (lambda)>

> lamb_derr.lambda? => true

Page 120: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Lambdas are

Procs

Page 121: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

A Trick

Page 122: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

posts = Post.all posts.map do |p| p.title end

# => [ # "Procs are fun", # "Yay, Procs!" # ]

Page 123: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

posts = Post.all posts.map(&:title)

# => [ # "Procs are fun", # "Yay, Procs!" # ]

Page 124: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

posts = Post.all posts.map(&:title)

# => [ # "Procs are fun", # "Yay, Procs!" # ]

Page 125: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

posts = Post.all posts.map(&:title)

# => [ # "Procs are fun", # "Yay, Procs!" # ]

Page 126: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

&:title

Page 127: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

&:title => :title.to_proc

Page 128: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Symbol # ...

def to_proc proc {|x| x.send(self)} end

end

Page 129: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Symbol # ...

def to_proc proc {|x| x.send(self)} end

end

Page 130: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Symbol # ...

def to_proc proc {|x| x.send(self)} end

end

Page 131: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

class Symbol # ...

def to_proc proc {|x| x.send(self)} end

end

Page 132: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

&:title => :title.to_proc

Page 133: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

&:title => :title.to_proc => proc {|x| x.send(:title) }

Page 134: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

&:title => :title.to_proc => proc {|x| x.send(:title) }

# Which makes our # original call:

Page 135: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

&:title => :title.to_proc => proc {|x| x.send(:title) }

# Which makes our # original call: posts.map(&( proc{|x| x.send(:title)} ))

Page 136: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

posts = Post.all posts.map(&:title)

# => [ # "Procs are fun", # "Yay, Procs!" # ]

Page 137: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Summing Up

Page 138: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Blocks: Portable chunks of

Ruby code.

Page 139: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Procs: An object with a call()

method that runs a Block.

Page 140: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Lambdas: Procs (with a flag) that

has return to pretend to bea regular method.

Page 141: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Ruby: Re-open Symbol to

add to_proc for #YOLO #SWAG

Page 142: Rorosyd 2018 - Blocks, Procs and Lambdas - Rob …...def greet(msg) if block_given? who = yield puts "#{msg}, #{who}!" else puts "#{msg}!" end end greet("Hello") do "Ruby meetup" end

Rob Howard@damncabbagehttp://robhoward.id.au

Blocks, Procsand Lambdas