Created
June 20, 2012 00:18
-
-
Save avand/2957288 to your computer and use it in GitHub Desktop.
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
class Car | |
attr_accessor :brand | |
def a | |
if false | |
# brand = nil | |
else | |
brand | |
end | |
end | |
def b | |
if false | |
brand = nil | |
else | |
brand | |
end | |
end | |
end | |
car = Car.new | |
car.brand = "Subaru" | |
puts car.a # => "Subaru" | |
puts car.b # => nil (WTF?!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's not just the
brand = nil
causing a local variable definition at parse time, that's causing your confusion however.Building out our example more may provide you another piece of the puzzle that you're missing.
The
attr_accessor :brand
is being defined on theCar
's metaclass, causing you to technically need to alter it by callingself.brand=
and not justbrand=
which is seen as a local variable definition. Note thatself.brand=
is very different thanself.class.brand=
which would be calling a class method not an instance method.