Created
November 30, 2012 21:31
-
-
Save tatat/4178794 to your computer and use it in GitHub Desktop.
POSTするとき nyan.nyan1=nyan&nyan.nyan2=nyan って書きたかった
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 fold!(separator = '.') | |
folder = {} | |
each do |key, value| | |
next unless key.is_a? String | |
dir = key.split separator, -1 | |
next if dir.length == 1 | |
dir_last = dir.pop | |
cd = folder | |
dir.each do |d| | |
cd[d] = {} unless cd[d].is_a? Hash | |
cd = cd[d] | |
end | |
cd[dir_last] = value | |
delete key | |
end | |
merge! folder | |
self | |
end | |
end |
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
nyan = { | |
'nyan.nyan' => 'nyan.nyan', | |
'nyan.nyan1' => 'nyan.nyan1', | |
'nyan.nyan2' => 'nyan.nyan2', | |
'nyan.nyan.nyan4' => 'nyan.nyan.nyan4', | |
'nyan.nyan.nyan5' => 'nyan.nyan.nyan5' | |
} | |
p nyan.fold! | |
# { | |
# "nyan" => { | |
# "nyan" => { | |
# "nyan4" => "nyan.nyan.nyan4", | |
# "nyan5" => "nyan.nyan.nyan5" | |
# }, | |
# "nyan1" => "nyan.nyan1", | |
# "nyan2" => "nyan.nyan2" | |
# } | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment