【发布时间】:2009-12-01 23:14:13
【问题描述】:
我发现自己在 Ruby 中经常使用类似 PHP 的循环,而当语言的其余部分如此简洁时,我感觉很不对劲。我结束了这样的代码:
conditions_string = ''
zips.each_with_index do |zip, i|
conditions_string << ' OR ' if i > 0
conditions_string << "npa = ?"
end
# Now I can do something with conditions string
我觉得我应该能够做这样的事情
conditions_string = zips.each_with_index do |zip, i|
<< ' OR ' if i > 0
<< "npa = ?"
end
在 Ruby 中是否有一种“简洁”的方式来设置带有块的变量?
【问题讨论】:
-
我想每个人都没有抓住重点。我也一直想知道这一点,还有其他几种语言可以做到这一点,即 perl 的 $_。使用 $_