Proc、proc、lambda、yield self

创建Proc对象

  • Proc.new
 p = Proc.new { |m| m+1 }
p.call(3) #=> 4
  • proc
p = proc { |m| m+ 1 }
p.call(3) #=>4
  • lambda
la = lambda { |m| m + 1 }
la.class #=> Proc
la.call(3) # => 4
  • ->
p = ->(m) { m + 1}
p.call(3) #=> 4

&,将代码块作为非匿名参数传递

def math(a,b)
   yield(a,b)
end
def do_math(a,b,&operation)
   math(a,b,&operation) 
end
do_math(2,3) { |x,y| x + y} #=>5
  • &操作符的含义
def my_method(&my_proc)
   my_proc
end
p = my_method { |name|  "Hello, #{name.capitalize}" }
p.call("getsuKami") # => Hello, GetsuKami

&操作符的含义是:这是一个Proc对象,我想把他当成代码块来使用。最掉&操作符,就能再次得到一个Proc对象。

def my_method(name)
 "#{name}, #{yield}"
end
my_proc = proc { "Hello" }
my_method("Bob", &my_proc) # => Bob,Hello

Proc与Lambda的对比

用Lambda创建的Proc叫lambda,用其他方式创建的Proc称为proc。
可以使用Proc#lambda?来检测Proc是不是lambda
使用->方法创建之后: p.lambda? 返回为True

  • proc 与lambda ,return的区别

proc 会从定义proc的作用域中返回,而lambda只是从lambda中返回。

def double(my_lambda)
   my_lambda.call *  2
end

def an_double
   p = Proc.new { return 10 }  #在这定义
   result = p.call  #在这里程序就被返回,就是从an_double的作用域返回
   return result * 2  #
end

def ann_double(my_proc)
   my_proc.call * 2
end

la = lambda { return 10 }
double(la) #=>20

an_double #=> 10

p = Proc.new { return 10 } #定义在main中
# ann_double(p) #=> error,不能main里面返回
  • 参数

lambda会严格检查参数。
proc不会,如果多了,就忽略后面多的,如果少了,就用nil代替。

yield and self

yield 对代码块的调用
self 对当前对象的引用

class A
  def hello1
    yield self if block_given?
  end
  def hello2
    yield 1 if block_given?
  end
  def self.hello
    yield self if block_given?
  end
end

A.new.hello1 { |var| p var} #=>#<A:0x0000000147eef0>
A.new.hello2 { |var| p var} # => 1
A.hello { |var| p var } # => A

self 就是作为一个参数传递进去而已。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容