Proc.new , proc , lambda and "store values of local variables within the scope in which the block was created"

简介:
Proc.new, proc, lambda 用于从block创建对象.
Ruby 1.8 中 , proc, lambda 都需要检查参数个数. Proc.new 不检查参数个数.
Ruby 1.9 中 , lambda 需要检查参数个数. Proc.new和proc 不检查参数个数.
block中调用的本地变量, 取自创建这个块时所在的本地变量, 而不是调用块时的本地变量.
下面都是Ruby 1.9 中的举例,
 举例一 :
x = "hello world"
ablock = lambda{ |y| puts(x) }  
# 如果上面这行改成 ablock = lambda{ puts(x) }, 调用ablock都将报错, wrong number of arguments (1 for 0 (ArgumentError)
def aMethod(ablockArg)
  x = "goodbye"
  ablockArg.call(x)
end

puts(x) #=> "hello world"
ablock.call(x) #=> "hello world"
aMethod(ablock) #=> "hello world"
ablock.call(x) #=> "hello world"
puts(x) #=> "hello world"
输出都是 "hello world" 没有一个是goodbye, 原因是 " block中调用的本地变量, 取自创建这个块时所在的本地变量, 而不是调用块时的本地变量. "


 举例二 : 
x = "hello world"
ablock = proc{ puts(x) }
# proc 在1.9版本中不用检查参数个数, 所以不会报错
def aMethod(ablockArg)
  x = "goodbye"
  ablockArg.call(x)
end

puts(x) #=> "hello world"
ablock.call(x) #=> "hello world"
aMethod(ablock) #=> "hello world"
ablock.call(x) #=> "hello world"
puts(x) #=> "hello world"


 举例三 :
x = "hello world"
ablock = Proc.new{ puts(x) }
Proc.new创建的块对象在调用是也不用检查传入的参数个数.
def aMethod(ablockArg)
  x = "goodbye"
  ablockArg.call(x)
end

puts(x) #=> "hello world"
ablock.call(x) #=> "hello world"
aMethod(ablock) #=> "hello world"
ablock.call(x) #=> "hello world"
puts(x) #=> "hello world"


 举例四 : 

严格来说, 上面的代码应该改成如下 :
x = "hello world"
ablock = lambda{ || puts(x) } # 表示不需要传入参数
# 或者 ablock = proc{ || puts(x) }
# 或者 ablock = Proc.new{ || puts(x) }
def aMethod(ablockArg)
  x = "goodbye"
  ablockArg.call()
end

puts(x) #=> "hello world"
ablock.call() #=> "hello world" # 调用时没有传入参数, 所以检查参数个数时是正确的.
aMethod(ablock) #=> "hello world"
ablock.call() #=> "hello world"
puts(x) #=> "hello world"


相关文章
|
8月前
|
调度 索引
NR PUCCH(四) UL data operation
UE 在connected mode 需要实时和网络进行上下行通信,在UE有UL data要发送但是没有UL grant时,就需要向网络端发送SR请求资源,网络收到SR就会在激活的BWP上发送 UL DCI给UE,UE 根据UL DCI 信息 获得UL grant ,然后在PUSCH对应的资源上就可以发送UL data给网络,最后网络端通过HARQ 过程指示是否有收到对应的data。这是UL data 的基本流程,下面通过实际log分别看下UL data operation的各个过程。
|
5月前
SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘thinkphp.test‘ don‘t exsit
SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘thinkphp.test‘ don‘t exsit
90 0
|
11月前
加载模型出现-RuntimeError: Error(s) in loading state_dict for Net:unexpected key(s) in state_dict: XXX
加载模型出现-RuntimeError: Error(s) in loading state_dict for Net:unexpected key(s) in state_dict: XXX
360 0
MGA (Managed Global Area) Reference Note (Doc ID 2638904.1)
MGA (Managed Global Area) Reference Note (Doc ID 2638904.1)
222 0
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
537 0
torch.distributed.init_process_group(‘gloo’, init_method=‘file://tmp/somefile’, rank=0, world_size=1
custom field further usage - add into UI and report
custom field further usage - add into UI and report
113 0
custom field further usage - add into UI and report
Global variable in ABAP function group
Global variable in ABAP function group
146 0
Global variable in ABAP function group
|
SQL 存储 Oracle
Implementation of Global Temp Table
作者| 曾文旌阿里云数据库高级技术专家
332 0
Implementation of Global Temp Table