[ruby-core:101912] [Ruby master Bug#17509] Custom respond_to? methods in modules break defined?(super)
From:
benedikt@...
Date:
2021-01-04 14:48:15 UTC
List:
ruby-core #101912
Issue #17509 has been reported by benediktdeicke (Benedikt Deicke).
----------------------------------------
Bug #17509: Custom respond_to? methods in modules break defined?(super)
https://bugs.ruby-lang.org/issues/17509
* Author: benediktdeicke (Benedikt Deicke)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
When using `defined?(super)` to check that a superclass method exists before calling `super`, including modules with a custom `respond_to?` method seems to break the check so it wrongfully returns a truthy value, even though the superclass method doesn't exist.
This only happens on Ruby 3.0.0 and works fine on previous Ruby versions. This is possibly related to this change: https://github.com/ruby/ruby/pull/3777
### Example
```
module SomeModule
def a_method
defined?(super) ? super : :no_super_method
end
end
module RespondModule
def respond_to?(*)
super
end
end
class PlainClass
include SomeModule
end
class ModuleClass
include RespondModule
include SomeModule
end
puts "PlainClass: #{PlainClass.new.a_method}"
puts "ModuleClass: #{ModuleClass.new.a_method}"
```
### Actual Result on Ruby 3.0.0p0
```
PlainClass: no_super_method
test.rb:3:in `a_method': super: no superclass method `a_method' for #<ModuleClass:0x00007fd77383f908> (NoMethodError)
Did you mean? method
from test.rb:24:in `<main>'
```
### Expected Result (like on older versions)
```
PlainClass: no_super_method
ModuleClass: no_super_method
```
Thanks to Rafael Fran軋 for helping me unravel this.
---Files--------------------------------
test.rb (359 Bytes)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>