Created
December 16, 2010 08:20
-
-
Save deepak/743193 to your computer and use it in GitHub Desktop.
ruby 1.9 eval and AST creates varibales when encountered
This file contains hidden or 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
| #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