我想知道是否可以從傳遞給Each的塊中放置的for循環中返回到Ruby中的每個迭代器。從For循環返回到每個迭代器
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循環?你爲什麼不用塊來做每種方法? – jason328 2013-04-30 23:36:16
@ jason328這會有幫助嗎?我相信這個問題仍然是一樣的。我在這裏使用for循環的原因是,爲了與參數「member」中的相應字符進行比較,我可以跟蹤索引號。我想概念上我的問題是,任何返回語句(例如break,next,return)是否區分for循環迭代器和每個迭代器。 – voltair 2013-04-30 23:44:55
我很難理解你的問題。會使用'each_with_index'幫助你嗎? – jason328 2013-04-30 23:51:47