Bug #18752
closeddefined? String.instance_method(:abcdefg) will return a "method" string instead nil.
Description
Please check following IRB example which can be reproduce on ruby 3.1.0
3.1.0 :012 > defined? String.instance_method(:abcdefg)
=> "method"
3.1.0 :013 > defined? String.instance_method(:abcdefg111111111111)
=> "method"
3.1.0 :014 > defined? String.instance_method(:abcdefg22222222222)
=> "method"
3.1.0 :015 > defined? String.instance_method(:what_ever_not_exists_method_name_always_return_method?)
=> "method"
3.1.0 :016 > String.instance_method(:what_ever_not_exists_method_name_always_return_method?)
(irb):16:in `instance_method': undefined method `what_ever_not_exists_method_name_always_return_method?' for class `String' (NameError)
from (irb):16:in `<main>'
from /home/common/.rvm/rubies/ruby-3.1.0/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
from /home/zw963/.rvm/rubies/ruby-3.1.0/bin/irb:25:in `load'
from /home/zw963/.rvm/rubies/ruby-3.1.0/bin/irb:25:in `<main>'
3.1.0 :017 >
3.1.0 :018 > defined? AAA
=> nil
3.1.0 :019 > AAA
(irb):19:in `<main>': uninitialized constant AAA (NameError)
from /home/common/.rvm/rubies/ruby-3.1.0/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
from /home/zw963/.rvm/rubies/ruby-3.1.0/bin/irb:25:in `load'
from /home/zw963/.rvm/rubies/ruby-3.1.0/bin/irb:25:in `<main>'
3.1.0 :020 >
If the method not be defined on String#, should it return nil instead, as const does, right?
Updated by chrisseaton (Chris Seaton) over 2 years ago
Seems as expected to me.
The defined? keyword when called with a method name having a module as receiver returns 'method' if the method is defined
Note that the 'method' in question is instance_method
.
Updated by chrisseaton (Chris Seaton) over 2 years ago
I've proposed some new specs to clarify something you might be confused about.
The defined? keyword when called with a method name does not execute the method
and
The defined? keyword when called with a method name does not execute the arguments
So if you thought String.instance_method(:abcdefg)
was being executed, that's where you're mistaken.
Updated by Eregon (Benoit Daloze) over 2 years ago
- Status changed from Open to Rejected
Expected behavior as Chris said.
Updated by zw963 (Wei Zheng) over 2 years ago
chrisseaton (Chris Seaton) wrote in #note-2:
I've proposed some new specs to clarify something you might be confused about.
The defined? keyword when called with a method name does not execute the method
and
The defined? keyword when called with a method name does not execute the arguments
So if you thought
String.instance_method(:abcdefg)
was being executed, that's where you're mistaken.
So if you thought
String.instance_method(:abcdefg)
was being executed, that's where you're mistaken.
Yes, that is my mistake, thank you very much.