Skip to content

Instantly share code, notes, and snippets.

@kch
Created January 26, 2010 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kch/287358 to your computer and use it in GitHub Desktop.
Save kch/287358 to your computer and use it in GitHub Desktop.
yet another annoying ruby quiz. now solved.
#!/usr/bin/env ruby1.9
# encoding: UTF-8
###############################################################################
#### The quiz: turn this hash… ################################################
###############################################################################
def initial_hash
{ 'child.name' => "a",
'child.surname' => "b",
'child.sex' => "c",
'child.birth.location' => "d",
'child.father.name' => "e",
'child.mother.name' => "f", }
end
###############################################################################
#### … into a hash that's suitable for output with the following function #####
###############################################################################
def puts_final_hash(final_hash)
puts final_hash['child']['name'],
final_hash['child']['surname'],
final_hash['child']['sex'],
final_hash['child']['birth']['location'],
final_hash['child']['father']['name'],
final_hash['child']['mother']['name']
end
#!/usr/bin/env ruby1.9
# encoding: UTF-8
$: << "."
require 'quiz'
final_hash = raise NotImplementedError
puts_final_hash(final_hash)
#!/usr/bin/env ruby1.9
# encoding: UTF-8
$: << "."
require 'quiz'
def λ(&b); lambda(&b); end # <= This is just icing on the cake, you can do without it.
# My favorite version is single line. This is how I actually wrote it.
# … err, which I'm now breaking in two for better github presentation
final_hash = initial_hash.inject({}) { |h, (jk, v)|
jk.split(".").inject(λ{|_|h}) { |ƒ, k| λ{ |v| ƒ[{}][k] ||= v } }[v]; h }
# print beats unit tests!
puts_final_hash(final_hash)
###############################################################################
###############################################################################
###############################################################################
# Other presentations of the same thing #######################################
###############################################################################
###############################################################################
###############################################################################
###############################################################################
# Here's the lisper-style multiline version ###################################
###############################################################################
final_hash =
initial_hash.inject({}) { |h, (jk, v)|
jk.split(".").inject(λ{|_|h}) { |ƒ, k|
λ{ |v| ƒ[{}][k] ||= v } }[v]; h }
###############################################################################
# Here's it verbosely layed out for dummies. ##################################
# I find this shit totally unreadable. ##################################
###############################################################################
final_hash = initial_hash.inject(Hash.new) do |return_hash, (joint_keys, value)|
joint_keys.split(".").inject(lambda { |unused_arg| return_hash }) do |f_hash, key|
lambda { |local_value| f_hash.call(Hash.new)[key] ||= local_value }
end.call(value)
return_hash
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment