Bug #8817

Method#owner on prepended classes is an instance rather than Class/Method

Added by Bradley Schaefer about 3 years ago. Updated over 2 years ago.

ruby -v:
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]


rdoc indicates that Method#owner will return a class or module that defines a method.

For classes that have prepended modules, the owner is actually an instance.

Example code:

module Mod; end

class Foo
prepend Mod
def foo; 'foo'; end

owner = Foo.instance_method(:foo).owner
puts "Method#owner on a prepended class instance_method: #{owner.inspect}" #=> #Foo:0x007fd34dfd3ea0
puts "Class?: #{owner.is_a? Class}" #=> false
puts "Module?: #{owner.is_a? Module}" #=> false

This had the effect of breaking stubbing in rspec ( , though a workaround is that the expected Class/Module can be retrieved by asking for the returned object's class.

Related issues

Duplicates Ruby trunk - Bug #7993: owner of methods defined after using Module#prepend Closed 03/01/2013


#1 [ruby-core:59158] Updated by Jack Nagel almost 3 years ago

This appears to have been fixed by r44175.

Can it be backported (perhaps with a regression test for this specific bug)?

#2 [ruby-core:59174] Updated by Nobuyoshi Nakada almost 3 years ago

  • Status changed from Open to Closed
  • Backport changed from 1.9.3: UNKNOWN, 2.0.0: UNKNOWN to 1.9.3: DONTNEED, 2.0.0: REQUIRED

Thank you, I missed this ticket.

#3 [ruby-core:60873] Updated by Tomoyuki Chikanaga over 2 years ago

  • Backport changed from 1.9.3: DONTNEED, 2.0.0: REQUIRED to 1.9.3: DONTNEED, 2.0.0: DONE

sorry for my late response. r44175 and r44179 were already backported to ruby_2_0_0. see #7993

Also available in: Atom PDF