Skip to content

Instantly share code, notes, and snippets.

@deepak
Created December 16, 2010 08:20
Show Gist options
  • Select an option

  • Save deepak/743193 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/743193 to your computer and use it in GitHub Desktop.
ruby 1.9 eval and AST creates varibales when encountered
#dkannan: raggi: just copy-pasted the whole thing 1:04 PM
#raggi: the defined?(o = something) is basically a bug i think 1:05 PM
#raggi; not 100% on that, but i think it's an AST bug, you can do a similar thing in other ways 1:06 PM
#raggi: (that is, you can make local variables come into existence without assigning to them)
# short-circuit
puts defined? foo
false && (foo = "will this be assigned")
puts defined? foo
puts foo.inspect
# asssignment - raggi's code
b = :b if b
local_varaibles # => ['b']
# in 1.9 eval does not modify the enclosing scope, have to pass it an explicit binding
def test bool
eval('a = 1; puts a;') if bool
# what about functions
puts "a in env: #{a}" rescue nil
return local_variables, defined?(a), eval('defined?(a)')
end
p test(true)
p test(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment