【发布时间】:2013-04-24 23:58:52
【问题描述】:
我想知道是否可以在传递给 Each 的块中的 for 循环中返回 Ruby 中的 Each 迭代器。
def find member = ""
productHash = {}
#@entries is a hash, with both the keys and values being strings
#the member parameter is a string
@entries.each do |key, value|
for i in 0...member.size
if(key[i] != member[i])
next #the next keyword doesn't work...all it does is return to the for iterator. I'm looking for a keyword that would return to the each iterator, and allow each to pass back in the next key-value pair.
end
end
productHash[key] = value
end
productHash
end
我想要完成的是:当我看到成员参数中的一个字符与给定键中的相应字符不匹配时,我会转到下一个键值对。
【问题讨论】:
-
为什么要使用 for 循环?你为什么不用一个块做一个 each 方法?
-
@jason328 这有什么帮助?我相信这个问题仍然是一样的。我在这里使用 for 循环的原因是我可以跟踪索引号,以便与参数“member”中的相应字符进行比较。我想从概念上讲,我的问题是是否有任何 return 语句(例如 break、next、return)区分 for 循环迭代器和 each 迭代器。
-
我很难理解你的问题。使用
each_with_index对你有帮助吗? -
什么是
member?一个数组? -
看起来像。我怀疑这是伪代码,因为如果不添加一些参数,该方法就无法工作。