我刚看了下面的代码:
class Dir
def self.create_uniq &b ### Here, & should mean b is a block
u = 0
loop do
begin
fn = b[u] ### But, what does b[u] mean? And b is not called.
FileUtils.mkdir fn
return fn
rescue Errno::EEXIST
u += 1
end
end
io
end
end
我把我的困惑作为评论在代码中。
定义方法 &b
最后允许你使用传递给方法的块作为 Proc
目的。
现在,如果你有 Proc
例如, []
语法是简写 call
:
p = Proc.new { |u| puts u }
p['some string']
# some string
# => nil
记录在这里 - > Proc#[]
定义方法 &b
最后允许你使用传递给方法的块作为 Proc
目的。
现在,如果你有 Proc
例如, []
语法是简写 call
:
p = Proc.new { |u| puts u }
p['some string']
# some string
# => nil
记录在这里 - > Proc#[]
&前缀运算符允许方法将传递的块捕获为命名参数。例如:
def wrap &b
3.times(&b)
print "\n"
end
现在如果您调用上面的方法,如下所示:
wrap { print "Hi " }
然后输出将是:
Hi Hi Hi