ruby Ruby Proc,Lambda和Closure

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby Proc,Lambda和Closure相关的知识,希望对你有一定的参考价值。

#!/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

以上是关于ruby Ruby Proc,Lambda和Closure的主要内容,如果未能解决你的问题,请参考以下文章

ruby中proc和lambda的return区别

Ruby:将proc转换为lambda?

Ruby - lambda 与 Proc.new [重复]

Ruby:如何获取可选 proc 参数的默认值

使用 lambda 在单个实例上重新定义单个 ruby​​ 方法

为啥我应该使用 lambda/proc ?解释它的重要性 [重复]