如果我有
footnotes = { "apple" => "is a fruit", "cat" => "is an animal", "car" => "a transport"}如何才能获得这些内容的索引?,例如:
footnotes["cat"].index
# => 1发布于 2015-03-11 23:08:05
不需要首先检索密钥:
footnotes.find_index { |k,_| k== 'cat' } #=> 1至于哈希键-值对是否有索引的问题(从v1.9开始),我只想指出,Ruby的修道士决定为我们提供Enumerable#find_index,这当然意味着Hash.instance_methods.include?(:find_index) #=> true。
发布于 2015-03-11 22:48:02
你可以做到
footnotes.to_a.index {|key,| key == 'cat'}
# => 1https://stackoverflow.com/questions/28989280
复制相似问题