Skip to content

Instantly share code, notes, and snippets.

@srpouyet
Created October 29, 2012 19:23
Show Gist options
  • Save srpouyet/3975909 to your computer and use it in GitHub Desktop.
Save srpouyet/3975909 to your computer and use it in GitHub Desktop.
Functional programming babysteps :)
# 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