Project

General

Profile

Actions

Bug #18752

closed

defined? String.instance_method(:abcdefg) will return a "method" string instead nil.

Added by zw963 (Wei Zheng) almost 2 years ago. Updated almost 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:108380]

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) almost 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

https://github.com/ruby/spec/blob/aaf998fb8c92c4e63ad423a2e7ca6e6921818c6e/language/defined_spec.rb#L74-L76

Note that the 'method' in question is instance_method.

Updated by chrisseaton (Chris Seaton) almost 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

https://github.com/chrisseaton/spec/blob/e3c9e25c8c676619f5ecd868ad0e0f39f8d38717/language/defined_spec.rb#L51-L59

So if you thought String.instance_method(:abcdefg) was being executed, that's where you're mistaken.

Updated by Eregon (Benoit Daloze) almost 2 years ago

  • Status changed from Open to Rejected

Expected behavior as Chris said.

Updated by zw963 (Wei Zheng) almost 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

https://github.com/chrisseaton/spec/blob/e3c9e25c8c676619f5ecd868ad0e0f39f8d38717/language/defined_spec.rb#L51-L59

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0