Skip to content

Instantly share code, notes, and snippets.

@lsegal
Last active August 29, 2015 14:07
Show Gist options
  • Save lsegal/928c1ff1f68bd13b948c to your computer and use it in GitHub Desktop.
Save lsegal/928c1ff1f68bd13b948c to your computer and use it in GitHub Desktop.
class Foo
def foo; "abc" end
# this default param should resolve at runtime to the #foo method call
def run(foo = foo)
p foo # print shadowed local var defaulting to attr value
end
end
puts "Testing #{RUBY_VERSION}:"
Foo.new.run
__END__
$ rvm all do ruby test_method_shadow.rb
Testing 1.8.7:
"abc"
Testing 1.9.2:
"abc"
Testing 1.9.3:
"abc"
Testing 2.0.0:
"abc"
Testing 2.1.1:
"abc"
Testing 2.1.2:
"abc"
Testing 2.1.3:
"abc"
Testing 2.2.0:
nil
$
@mipearson
Copy link

I think this is a good change - I was never sure what would happen if I did this.

A better change would be for it to throw an error.

@damncabbage
Copy link

def foo
  "abc"
end

foo = foo

puts "Testing #{RUBY_VERSION}:"
p foo

# Testing 2.0.0:
# nil

Well damn. I'd still consider this surprising, but I guess this makes it more consistent. Hmm. 😟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment