你总是可以编写自己的具体任务扩展到散列这为你做肮脏的工作:
class Hash
def recursive_find_by_key(key)
# Create a stack of hashes to search through for the needle which
# is initially this hash
stack = [ self ]
# So long as there are more haystacks to search...
while (to_search = stack.pop)
# ...keep searching for this particular key...
to_search.each do |k, v|
# ...and return the corresponding value if it is found.
return v if (k == key)
# If this value can be recursively searched...
if (v.respond_to?(:recursive_find_by_key))
# ...push that on to the list of places to search.
stack << v
end
end
end
end
end
你可以很简单地使用这样的:
h={:x=>1,:y=>2,:z=>{:a=>{:k=>"needle"}}}
puts h.recursive_find_by_key(:k).inspect
# => "needle"
h={:k=>"needle"}
puts h.recursive_find_by_key(:k).inspect
# => "needle"
puts h.recursive_find_by_key(:foo).inspect
# => nil
美丽。正是我在找的东西。 – 2011-01-16 02:12:01
你会在哪里放置扩展的“哈希”类(例如在一个gem中),以便其方法可用于其他类? – thoughtpunch 2011-05-22 03:02:08
你需要将它加载到某种初始化器中。宝石通常有一个主库文件,所以这可能是为了达到这个目的。要么在那里定义它,要么相应地“要求”。 – tadman 2011-05-24 14:04:38