Created
February 14, 2012 10:47
-
-
Save ukitazume/1825736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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