-
-
Save gideondk/3976147 to your computer and use it in GitHub Desktop.
Functional programming babysteps :) Code reduction ;-)
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
# Functional programming babysteps :) | |
# This Proc returns a "stringified" version of the object you passed it. | |
# Example: | |
# | |
# h = Hash[1,2,3,4,5,[8,9,{:age => 90},[[[[60,{"a" => "gho"}]]],{[:what, 0] => [4.0 => 88.7]}]]]4.0 => 88.7]}]]] | |
# => {1=>2, 3=>4, 5=>[8, 9, {:age=>90}, [[[[60, {"a"=>"gho"}]]], {[:what, 0]=>[{4.0=>88.7}]}]]} | |
# stringit[h] | |
#=> {"1"=>"2", "3"=>"4", "5"=> ["8","9",{"age"=>"90"},[[[["60", {"a"=>"gho"}]]], {["what", "0"]=>[{"4.0"=>"88.7"}]}]]} | |
stringit = Proc.new do |v| | |
case v | |
when Array then v.inject([]) {|b,a| b + [stringit[a]] } | |
when Hash then v.inject({}) {|b,(k,v)| b.merge(stringit[k] => stringit[v])} | |
else v.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice!