Project

General

Profile

Actions

Bug #11980

closed

External methods are appearing as instance methods for String

Bug #11980: External methods are appearing as instance methods for String

Added by danielpclark (Daniel P. Clark) almost 10 years ago. Updated over 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:72820]

Description

It appears we're having methods creep into the String scope.

"".methods.count # => 170 defined? "".a # => nil def a "foo" end "".methods.count # => 171 defined? "".a # => "method" "bar".a # => "foo" 

Updated by danielpclark (Daniel P. Clark) almost 10 years ago Actions #1 [ruby-core:72821]

It's not just String.

Array.instance_method(:a).owner # => Object 

Updated by danielpclark (Daniel P. Clark) almost 10 years ago Actions #2 [ruby-core:72822]

Those results were from IRB. Here's my results from a Ruby script.

"test_scope_creep.rb"

puts "".methods.count puts defined? "".a def a "foo" end puts "".methods.count puts defined? "".a puts String.private_method_defined? :a puts String.instance_method(:a).owner puts "".send :a puts Array.private_method_defined? :a puts Array.instance_method(:a).owner puts [].send :a 

Output:

170 170 true Object foo true Object foo 

It appears they are defined as private methods when run via ruby and in irb they run like they're not private methods.

Updated by codetodya (Sumit Mahamuni) almost 10 years ago Actions #3 [ruby-core:72867]

It's not really a bug.

When you define following method. It is added on Object class. And any class which inherits from Object class will have following method.

You can try calling following method on any class which inherits Object class, you should get same output.

def a "foo" end 

Updated by jeremyevans0 (Jeremy Evans) over 6 years ago Actions #4

  • Status changed from Open to Closed
Actions

Also available in: PDF Atom