Skip to content

Instantly share code, notes, and snippets.

@nickyp
Created October 25, 2010 16:17
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 nickyp/645229 to your computer and use it in GitHub Desktop.
Save nickyp/645229 to your computer and use it in GitHub Desktop.
experiment trying to destructuring-bind a hash into local variables
# 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