rorosyd 2018 - blocks, procs and lambdas - rob …...def greet(msg) if block_given? who = yield puts...
TRANSCRIPT
![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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/40.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/41.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/42.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/43.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/44.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/45.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/46.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/47.jpg)
[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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/48.jpg)
[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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/49.jpg)
[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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/50.jpg)
[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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/51.jpg)
[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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/52.jpg)
[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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/53.jpg)
[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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/54.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/55.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/56.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/57.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/58.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/59.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/60.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/61.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/62.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/63.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/64.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/65.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/66.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/67.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/68.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/69.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/70.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/71.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/72.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/73.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/74.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/75.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/76.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/77.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/78.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/79.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/80.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/81.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/82.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/83.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/84.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/85.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/86.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/87.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/88.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/89.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/90.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/91.jpg)
#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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/92.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/93.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/94.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/95.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/96.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/97.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/98.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/99.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/100.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/101.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/102.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/103.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/104.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/105.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/106.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/107.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/108.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/109.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/110.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/111.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/112.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/113.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/114.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/115.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/116.jpg)
> 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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/117.jpg)
> 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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/118.jpg)
> 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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/119.jpg)
> 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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/120.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/121.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/122.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/123.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/124.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/125.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/126.jpg)
&: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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/127.jpg)
&: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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/128.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/129.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/130.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/131.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/132.jpg)
&: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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/133.jpg)
&: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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/134.jpg)
&: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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/135.jpg)
&: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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/136.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/137.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/138.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/139.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/140.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/141.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081613/5fb5880cc45a0f290457c939/html5/thumbnails/142.jpg)
Rob Howard@damncabbagehttp://robhoward.id.au
Blocks, Procsand Lambdas