Created
October 29, 2012 19:23
-
-
Save srpouyet/3975909 to your computer and use it in GitHub Desktop.
Functional programming babysteps :)
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 | |
r = [] | |
v.each do |i| | |
r << stringit[i] | |
end | |
r | |
when Hash | |
r = Hash.new | |
v.each do |key, value| | |
r.store(stringit[key], stringit[value]) | |
end | |
r | |
else | |
v.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment