Ruby Blocks, Procs & Lambdas
Objectives

Blocks

# Define
while true do
"I'm inside a block of code in an infinite loooooooooo....p"
end
[1, 2, 3].each do |number|
"Hi! I'm the block body"
"And 'number' is my block parameter"
"'number' will take the value of each element of the array"
end
# Syntaxes - Without arguments
64.times { print "-" }
64.times do
print "-"
end
# Syntaxes - With arguments
(1..5).map { |num| num * 3 }
(1..5).map do |num|
num * 3
end
# Iterators - With_index
numbers = [1, 2, 3, 4, 5].collect.with_index do |number, index|
number + index
end
p numbers

Yield Keyword

# Yield
def block_test
puts "We're in the method!"
yield
puts "We're back in the method!"
end
block_test { puts ">>> We're in the block!" }
block_test { puts ">>> hi"}
# Yield with parameters
def block_test(name)
puts "In the method! Let's yield."
puts "Yielding to the block..."
yield("Kim")
yield(name)
puts "Block complete! Back in the method."
end
# Now call the method with your name!
block_test("Paulo") { |n| puts "My name is #{n}." }


# Console 👆
In the method! Let's yield.
Yielding to the block...
>>> My name is Kim.
>>> My name is Paulo.
Block complete! Back in the method.
# Yield example
def calc_area(shape, *args)
result = yield(*args)
"The area of #{shape} is #{result}"
end
square_area = calc_area("square", 3){ |side| side**2 }
triangle_area = calc_area("triangle", 3, 2) do |base, height|
base*height/2
end
p square_area
p triangle_area
Procs

# Syntaxes
cube = Proc.new { |x| x ** 3 }
n_cubes = [4, 5, 6].map(&cube)
p n_cubes
# Example
group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7]
group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0]
group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2]
over_4_feet = Proc.new { |x| x >= 4 }
can_ride_1 = group_1.select(&over_4_feet) # group_1.select { |x| x >= 4 }
can_ride_2 = group_2.select(&over_4_feet)
can_ride_3 = group_3.select(&over_4_feet)
p can_ride_1
p can_ride_2
p can_ride_3
# Methods , Yield & Procs
def greeter
yield
end
phrase = Proc.new { puts "Hello!"}
greeter(&phrase)
# Symbols, Meet Procs
numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
strings_array = numbers_array.map(&:to_s)
Call Method

# Call Method
test = Proc.new
test.call
# Example
# Without parameters
hi = Proc.new { puts 'Hello!' }
hi.call
# with parameters
hi = Proc.new { |n| n * 2 }
p hi.call(2)
Lambdas

# Syntaxes
lambda { |param| block }
# Same to Procs
lambda { puts "Hello!" }
Proc.new { puts "Hello!" }
# Same To procs
def demo
yield
end
my_lambda = lambda { puts "I'm the lambda!" }
demo(&my_lambda)
# Call a Lambda
def demo(a_lambda)
puts "I'm the method!"
a_lambda.call
end
my_lambda = lambda { puts "I'm the lambda!" }
demo(my_lambda)
# With &block
def demo(&block)
puts "I'm the method!"
block.call('Hi')
yield('Hello')
end
my_lambda = lambda { |t | puts "I'm the lambda! #{t}" }
demo(&my_lambda)
# Example
strings = ["leonardo", "donatello", "raphael", "michaelangelo"]
symbolize = lambda { |x| x.to_sym }
symbols = strings.collect(&symbolize)
p symbols
# Procs vs Lambda
def batman_ironman_proc
victory = Proc.new { return "Batman will win!" }
victory.call
"Iron Man will win!"
end
puts batman_ironman_proc
puts "-----------P-vs-L----------------"
def batman_ironman_lambda
victory = lambda { return "Batman will win!" }
victory.call
"Iron Man will win!"
end
puts batman_ironman_lambda
Ruby Blocks, Procs & Lambda
By Paulo Tijero
Ruby Blocks, Procs & Lambda
- 178