Further research:
class Foo
attr_accessor :foo
def bar(foo: self.foo)
foo
end
end
f = Foo.new
f.foo = 'bar'
f.bar
It seems that Ruby 2.1.0 “initializes” the local variable before evaluating the “right side” of this operator, therefore it is fooconsidered as a local variable on the right side and therefore evaluated as nil.
This experiment seems to confirm my hypothesis:
class Foo
attr_accessor :foo
def bar(foo: defined?(foo))
foo
end
end
Foo.new.bar
Foo.new.bar
source
share