Skip to content

Instantly share code, notes, and snippets.

@ukitazume
Created February 14, 2012 10:47
Show Gist options
  • Save ukitazume/1825736 to your computer and use it in GitHub Desktop.
Save ukitazume/1825736 to your computer and use it in GitHub Desktop.
class Hash
def values_classify
inject({}) do |options, (key, value)|
options[key] = value.class
options
end
end
def values_classify!
replace(self.values_classify)
end
def values_classify_recursive
inject({}) do |options, (key, value)|
if value.instance_of?(Hash)
options[key] = value.values_classify_recursive
else
options[key] = value.class
end
options
end
end
def replace_by_path(path, replae_value)
inject({}) do |options, (key, value)|
if path.instance_of?(Array) and path.first == key
path.shift
if path.empty?
options[key] = replae_value
else
options[key] = self[key].replace_by_path(path, replae_value)
end
else
options[key] = value
end
options
end
end
end
hash = {:aa => 'aa', :hash => {:foo => 'iaa'}, :bar => 'bar'}
p hash.replace_by_path([:hash, :foo], "replacemenet")
p hash.values_classify
p hash.values_classify_recursive
hash.values_classify!
p hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment