Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
例如,我想从具有相同开头的哈希中删除所有键
myhash[:x_key_a] myhash[:x_key_b] myhash[:x_key_c]
所以我想删除所有以xfrom开头的键myhash
x
myhash
除了遍历所有键之外,还有更好的方法吗?
myhash.delete_if{ |key, _| key.to_s.start_with?('x') }
myhash = {} myhash[:x_key_a] = 1 myhash[:x_key_b] = 2 myhash[:y_key_c] = 3 p myhash.delete_if{ |key, _| key.match(/^x/) } # => {:y_key_c=>3}