【发布时间】:2011-10-06 19:05:42
【问题描述】:
我最近开始学习 ruby,我了解到您可以使用具有这两种语法的代码块。但是我只是发现了一个我不明白的案例:
#my_hash is a hash in which the keys are strings and the values arrays, but dont think about the specifics fo the code
#if I run my code like this, it works perfectly
my_hash.each do |art|
puts mystring.gsub(art[0]).each {
art[1][rand(art[1].length) -1]
}
end
#but if I use this, it prints "Enumerator"
my_hash.each do |art|
puts mystring.gsub(art[0]).each do
art[1][rand(art[1].length) -1]
end
end
是因为你不能嵌套 do-end 对吗? 我正在使用 1.9
【问题讨论】:
-
不是答案,但我注意到您可以使用
|k,v|表单来简化哈希迭代器。
标签: ruby