我发现自己在 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 中使用块设置变量?