class Hash
def keys_to_sym
hash2 = {}
self.each_pair do |key, val|
hash2[key.to_sym] = (val.respond_to? :keys_to_sym ? val.keys_to_sym : val);
end
hash2
end
def replace_key!(old, new)
if (has_key?(old)) then
self[new] = self[old]
self.delete old
end
end
end
Refactorings
No refactoring yet !
Adam
July 7, 2010, July 07, 2010 04:40, permalink
The problem in your code is operator precedence. On line 5, you are essentially writing: value.respond_to?(:keys_to_sym ? val.keys_to_sym : val)
class Object
def keys_to_sym
self
end
end
class Hash
def keys_to_sym
inject({}) { |hash,(key,value)| hash.merge(key.to_sym => value.keys_to_sym) }
end
end
Fabien Jakimowicz
July 7, 2010, July 07, 2010 11:03, permalink
If your are in Rails project, take a look at HashWithIndifferentAccess
h = {:asdf => 'toto', :titi => 42}
=> {:asdf=>"toto", :titi=>42}
h2 = HashWithIndifferentAccess.new(h)
=> {"titi"=>42, "asdf"=>"toto"}
h2[:titi]
=> 42
h2['titi']
=> 42
Hash#keys_to_sym is broken. Runtime throws:
undefined method `keys_to_sym' for #<Array:0x8d63550>
/home/lsiden/projects/alc-datasource/model.rb:28:in `block in keys_to_sym'
/home/lsiden/projects/alc-datasource/model.rb:27:in `each_pair'
/home/lsiden/projects/alc-datasource/model.rb:27:in `keys_to_sym'
Why does runtime think that val is an array if val.kind_of? Hash evals as true?