Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/f487a9c20043e3afc732 to your computer and use it in GitHub Desktop.
Save myronmarston/f487a9c20043e3afc732 to your computer and use it in GitHub Desktop.
Example of a weird ruby bug (https://bugs.ruby-lang.org/issues/9967)
puts RUBY_DESCRIPTION
class SuperClass
def foo
"foo"
end
end
class SubClass < SuperClass
def foo(arg)
arg.bar { super() }
end
end
class Arg
def bar(&block)
Class.new { define_method(:some_method, &block) } if $define_method
yield
end
end
print "Without define_method: "
puts SubClass.new.foo(Arg.new)
$define_method = true
print "With define_method: "
puts SubClass.new.foo(Arg.new)
➜ code chruby 2.1.1
➜ code ruby block_weirdness.rb
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0]
Without define_method: foo
With define_method: block_weirdness.rb:11:in `block in foo': super called outside of method (NoMethodError)
from block_weirdness.rb:18:in `bar'
from block_weirdness.rb:11:in `foo'
from block_weirdness.rb:27:in `<main>'
➜ code chruby 2.0
➜ code ruby block_weirdness.rb
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]
Without define_method: foo
With define_method: block_weirdness.rb:11:in `block in foo': super called outside of method (NoMethodError)
from block_weirdness.rb:18:in `bar'
from block_weirdness.rb:11:in `foo'
from block_weirdness.rb:27:in `<main>'
➜ code chruby 1.9.3
➜ code ruby block_weirdness.rb
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]
Without define_method: foo
With define_method: block_weirdness.rb:11:in `block in foo': super called outside of method (NoMethodError)
from block_weirdness.rb:18:in `bar'
from block_weirdness.rb:11:in `foo'
from block_weirdness.rb:27:in `<main>'
➜ code chruby 1.9.2
➜ code ruby block_weirdness.rb
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.4.0]
Without define_method: foo
With define_method: block_weirdness.rb:11:in `block in foo': super called outside of method (NoMethodError)
from block_weirdness.rb:18:in `bar'
from block_weirdness.rb:11:in `foo'
from block_weirdness.rb:27:in `<main>'
➜ code chruby 1.8.7
➜ code ruby block_weirdness.rb
ruby 1.8.7 (2013-06-27 patchlevel 374) [i686-darwin12.4.0]
Without define_method: foo
With define_method: foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment