Last active
August 29, 2015 14:15
-
-
Save gongo/ab19c995f58b55a930ba to your computer and use it in GitHub Desktop.
`foo` redefined as local variable. Why?
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
def foo | |
12 | |
end | |
p '----------' | |
p "foo is #{defined?(foo)}" | |
p "bar is #{defined?(bar)}" | |
p '----------' | |
if defined?(bar) | |
p "In `if` block" | |
foo = bar | |
end | |
p "foo is #{defined?(foo)}" | |
p "bar is #{defined?(bar)}" | |
p '----------' | |
# "----------" | |
# "foo is method" | |
# "bar is " | |
# "----------" | |
# "foo is local-variable" | |
# "bar is " | |
# "----------" |
http://docs.ruby-lang.org/ja/2.2.0/doc/spec=2fvariables.html#local
宣言は、例え実行されなくても宣言とみなされます。
ということなので foo = bar
実行されなくとも、foo の宣言
が有効であるため、とのこと。
- https://twitter.com/takkanm/status/566422158818029568
- https://twitter.com/hanachin_/status/566426422529376257
Thanks!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://docs.ruby-lang.org/ja/2.1.0/doc/spec=2fdef.html#defined
ここらへんと関わりあるんだろうか…