#!/usr/bin/env ruby
def run_yield
yield if block_given?
end
run_yield do
puts 'running yield'
end
#do nothing
run_yield
#yield block variable lifecycle
class Array
def random_each_yield
shuffle.each do |el|
yield el
end
end
def random_each_block(&block)
shuffle.each do |el|
block.call el
end
end
end
puts 'random each yield'
[1,2,3,4,5].random_each_yield do |el|
puts el
end
puts 'random each block'
[1,2,3,4,5].random_each_block do |el|
puts el
end
#pass proc as parameters
def run_procs(p1, p2)
p1.call
p2.call
end
proc_one = proc { puts 'run proc one' }
proc_two = proc { puts 'run proc two' }
run_procs proc_one, proc_two
#calling a proc in different ways:
num_proc = proc { |num| puts "The number is #{num} " }
num_proc.call 10
num_proc.(20)
num_proc[30]
num_proc === 40
#using proc in case when
even = proc { |num| num.even? }
odd = proc { |num| num.odd? }
0.upto(10) do | num |
case num
when even
puts "#{num} is even"
when odd
puts "#{num} is odd"
else
puts "Oops!"
end
end
#proc and lambda
def run_a_proc(p)
puts 'start...'
p.call
puts 'end.'
end
#the lambda will be ignore
def run_couple
run_a_proc proc { puts 'I am a proc'; return }
run_a_proc lambda { puts 'I am a lambda'; return }
end
run_couple
#closure
puts 'ruby closure hold the variable reference'
def run_name_proc(p)
p.call
end
name = 'Lv'
print_a_name = proc { puts name }
name = 'lvjian'
#it will output lvjian
run_name_proc print_a_name
#return a lambda, like high-order function
puts 'high-order lambda'
def multiple_gen(m)
lambda do |n|
n * m
end
end
doubler = multiple_gen 2
tripler = multiple_gen 3
puts doubler[10]
puts tripler[10]
#lambda must be passed matched parameters
hello_proc = proc do |a, b|
puts 'hello proc'
end
hello_lambda = lambda do |a, b|
puts 'hello lambda'
end
hello_proc.call
#hello_lambda.call # occure exception
hello_lambda.call 1, 2