Created
October 25, 2010 16:17
-
-
Save nickyp/645229 to your computer and use it in GitHub Desktop.
experiment trying to destructuring-bind a hash into local variables
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
# see | |
# http://markmail.org/message/zeessthwisaozywf#query:Ruby%20local_variable_set+page:1+mid:zeessthwisaozywf+state:results | |
require 'rubygems' | |
require 'extensions/binding' | |
foo = "bar" | |
puts "foo: " + Kernel.binding["foo"] | |
puts "" | |
hash = { | |
:one => 1, | |
:two => 2, | |
:three => 3 | |
} | |
one, two, three = nil # need to set them to nil, can't refer to dynamic created local vars | |
puts "local vars: " + Kernel.binding.local_variables.inspect | |
puts "" | |
puts "destructuring bind with local vars:" | |
hash.keys.each {|k| | |
Kernel.binding[k.to_s] = hash[k] | |
} | |
puts "one:\t#{one}" | |
puts "two:\t#{two}" | |
puts "three:\t#{three}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment