本文介绍了bizzare调用Procs的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了一个示例代码:
$ $ p $ def callback(procs)
procs [:var_1]
放置继续
procs [:var_2]
结束
回叫(:var_1 => Proc.new {block_1},
:var_2 => Proc.new {block_2})
我找不出方括号 [:var_1]
表示。这是一种叫Porc / lambda的方式吗?我也很困惑,因为用于创建Procs的哈希式方式:
pre $ call $ callback(:start => new {putsCALLBACK的开始},
:finish => Proc.new {putsCALLBACK的结尾})
我很感谢这方面的任何帮助。
方法回调被构建,因此它正在接收哈希。这个hash存储每个key的proc,hance执行proc,你需要用hash来使用它的key来修改它:
hash = {:start => Proc.new {放置CALLBACK的开始},
:finish => Proc.new {putsCALLBACK的结尾}}
hash [:start]#=> proc对象,你可以调用`call`。
I found an example code:
def callback(procs)
procs[:var_1]
puts "Proceed"
procs[:var_2]
end
callback(:var_1 => Proc.new {block_1},
:var_2 => Proc.new {block_2})
I can't figure out what is in square brackets [:var_1]
means. Is this some way to call Porc/lambda? I'm confused also because of the Hash-like way for creating Procs in:
callback(:start => Proc.new {puts "The begining of the CALLBACK"},
:finish => Proc.new {puts "The ending of the CALLBACK"})
I would appreciate any help on this matter.
解决方案
The method callbacks is build so it is receiving a hash. This hash stores procs against each key, hance to execute the proc you need to fatch it from hash using its key:
hash = { :start => Proc.new {puts "The begining of the CALLBACK"},
:finish => Proc.new {puts "The ending of the CALLBACK"} }
hash[:start] #=> proc object you can call `call` on.
这篇关于bizzare调用Procs的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!