Sha256: fa3e203c86a66a98cc63f7750b20a7060985110d3ef4178ed5f5357c8dafe57a
Contents?: true
Size: 1.44 KB
Versions: 3
Compression:
Stored size: 1.44 KB
Contents
#-- # Adapted from Gavin Sinclair's graceous work #symbolify_keys. #++ require 'facet/hash/keys_to_sym' class Hash # Synonym for Hash#keys_to_symbol, but modifies the receiver in place (and returns it). # With a from_class parameter, limits conversion to only a certain class of keys. # It defaults to String --use nil to convert any key class. # # require 'facet/hash/keys_to_symbol' # # foo = { 'name'=>'Gavin', 'wife'=>:Lisa } # foo.keys_to_sym! #=> { :name=>"Gavin", :wife=>:Lisa } # foo.inspect #=> { :name=>"Gavin", :wife=>:Lisa } # def keys_to_sym!( from_class=String ) raise ArgumentError, "Parameter must be a class" unless from_class.kind_of?(Class) if from_class if from_class self.each_key{ |k| self[k.to_sym]=self.delete(k) if k.respond_to?(:to_sym) and k.class == from_class } else self.each_key{ |k| self[k.to_sym]=self.delete(k) if k.respond_to?(:to_sym) } end self end # Aliases for better Extensions and Rails compatibility alias_method( :symbolize_keys!, :keys_to_sym! ) #-- # Old names (deprecated): #alias_method( :keys_to_symbol, :keys_to_sym ) #alias_method( :keys_to_symbol!, :keys_to_sym! ) #++ #-- # Rails has these aliases too, but they are not very good for # gerenal use, IMHO. But perhaps someone can convince me otherwise. #alias_method( :to_options, :symbolize_keys ) #alias_method( :to_options!, :symbolize_keys! ) #++ end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
facets-0.7.0 | lib/facet/hash/keys_to_sym%21.rb |
facets-0.7.1 | lib/facet/hash/keys_to_sym%21.rb |
facets-0.7.2 | lib/facet/hash/keys_to_sym%21.rb |