I do not hope this behavior in some cases (e.g. handling http json response)
So, I have added clarifications.
module HashUtil refine Hash do def eager_except(*args) proc = Proc.new do |key| case when key.is_a?(Symbol) key.to_s when key.is_a?(String) key.to_sym else nil end end eager_args = (args + args.map(&proc)).compact except(*eager_args) end end end using HashUtil hash = { a: 'a', "b" => 'b' } hash.eager_except('a', :b)
※ Additional Information
after writing above, I found other ways.
convert all keys to characters using Hash # deep_symbolize_keys!
{"a" => "hoge"}.deep_symbolize_keys!.except(:a)
use ActiveSupport :: HashWithIndifferentAccess
{"a" => "hoge"}.with_indifferent_access.except(:a)
thanks
source share