Why #owner method gives the name of owner as Class, while `Class#new` has been overriden?

Code# 1 :

class Foo
def bar
12
end
end

class Bar < Foo
def bar
14
end
end

Bar.new.method(:bar).owner # => Bar

Here in above code, Bar class has overrode the method Foo#bar, so
owner is Bar. This is I got.

Code# 2:

Array.method(:new).owner # => Class

As per the doc
(Class: Array (Ruby 2.1.1)), ::new is
defined(overrode) inside the class Array. Then why still it is showing
the owner as Class ?

Regards,
Arup R.

#initialize is in Rdoc as #new documented.

On May 10, 2014, at 2:45 PM, Arup R. [email protected]
wrote:

14

As per the doc (Class: Array (Ruby 2.1.1)),
::new is defined(overrode) inside the class Array. Then why still it is
showing the owner as Class ?

Regards,
Arup R.

Think about this:

[1] pry(main)> Array.new.method(:shift).owner
=> Array < Object
[2] pry(main)> Array.method(:new).owner
=> Class < Module
[3] pry(main)> Array.new.class
=> Array < Object
[4] pry(main)> Array.class
=> Class < Module

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Hi Mike,

Okay, I might couldn’t explain what I asked . Again see this one -

class Foo
def self.bar
“I have been defined in #{self}”
end
end

class Bar < Foo
def self.bar
“I have been defined in #{self}”
end
end

Bar.method(:bar).owner # => #Class:Bar

Here method bar has been overridden inside the class Bar, that’s why
#owner showing the name of the class Bar. Same Array class also
overrode the method Class#new, why then #owner giving the output as
Class instead of Array. That’s my Question.

On Sunday, 11 May 2014 4:30 AM, Mike S. [email protected] wrote:

On May 10, 2014, at 2:45 PM, Arup R. [email protected]
wrote:

Code# 1 :

14
Code# 2:

Array.method(:new).owner # => Class

As per the doc (Class: Array (Ruby 2.1.1)),
::new is defined(overrode) inside the class Array. Then why still it is
showing the owner as Class ?

Regards,
Arup R.
Think about this:

[1] pry(main)> Array.new.method(:shift).owner
=> Array < Object
[2] pry(main)> Array.method(:new).owner
=> Class < Module
[3] pry(main)> Array.new.class
=> Array < Object
[4] pry(main)> Array.class
=> Class < Module

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

like i said before, and you would know it if you wouldn’t ignore me,

this is part of the rdoc code, read the comment

Having now read the method parameters and documentation modifiers, we

now know whether we have to rename #initialize to ::new

if meth.name == “initialize” && !meth.singleton then
if meth.dont_rename_initialize then
meth.visibility = :protected
else
meth.singleton = true
meth.name = “new”
meth.visibility = :public
end
end

Array does only define #initialize, not ::new, but #initialize is
documented in rdoc as ::new

Hans M. wrote in post #1145703:

like i said before, and you would know it if you wouldn’t ignore me,

this is part of the rdoc code, read the comment

Thanks for your explanation. I didn’t see your comments in mailing list.
I don’t know why. But I just read both of your comments, and got your
point, in Ruby Forum only. I got only @mike’s email in my email box.
Could you also give me the Rdoc link ?

Having now read the method parameters and documentation modifiers, we

now know whether we have to rename #initialize to ::new

if meth.name == “initialize” && !meth.singleton then
if meth.dont_rename_initialize then
meth.visibility = :protected
else
meth.singleton = true
meth.name = “new”
meth.visibility = :public
end
end

Array does only define #initialize, not ::new, but #initialize is
documented in rdoc as ::new

https://github.com/ruby/ruby/blob/trunk/lib/rdoc/parser/ruby.rb#L1336-L1362

+1 for Abinoam’s answer.

Thanks

Rajeev N B
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Sunday, May 11, 2014 09:32:56 PM you wrote:

Array.instance_of? Class # true
end
end

Bar.method(:new).owner
=> #Class:Bar # Bar metaclass

So! What is the problem with that?
Shouldn’t Array.method(:new).owner point to Array metaclass
#Class:Array instead of Class?
Exactly what I thought about it.

Yes! You should be right. But only if it did exactly what we did,
redefining Array.new (class method).
But it doesn’t do that.
What Array really do is to define Array#initialize (instance method).
It doesn’t mess with the Array.new (Inherited from Class#new).

I agree.

(Just toggle source code view on the link you provided.)

So when I do

a = Array.new

The method new calls Array.allocate and creates the object (instance
of Array) and call the freshly created object’s #initialize method.

Did it clarified the issue?

Yes it is.

Thanks for such detail explanation.

You’re welcome! :slight_smile:

On Mon, May 12, 2014 at 11:00 AM, Arup R.
[email protected] wrote:

Did it clarified the issue?

Yes it is.

Thanks for such detail explanation.

[Short answer: “Class#new was not overriden”]

Hi Arup,

Array superclass is not Class.

Array.superclass # Object

Array is an instance of Class instead.

Array.instance_of? Class # true

So you shouldn’t test it with “class Bar < Foo”.
I think you should test it like this…

Bar = Class.new # not necessary

class Bar
def self.new
puts “Running #new from eigen Bar with self #{self}”
super
end
end

Bar.method(:new).owner
=> #Class:Bar # Bar metaclass

So! What is the problem with that?
Shouldn’t Array.method(:new).owner point to Array metaclass
#Class:Array instead of Class?
Yes! You should be right. But only if it did exactly what we did,
redefining Array.new (class method).
But it doesn’t do that.
What Array really do is to define Array#initialize (instance method).
It doesn’t mess with the Array.new (Inherited from Class#new).
(Just toggle source code view on the link you provided.)

So when I do

a = Array.new

The method new calls Array.allocate and creates the object (instance
of Array) and call the freshly created object’s #initialize method.

Did it clarified the issue?

Abinoam Jr.

PS1: More on Class: Class (Ruby 2.1.1)
“Calls allocate to create a new object of class’s class, then invokes
that object’s initializemethod, passing it args. This is the method
that ends up getting called whenever an object is constructed using
.new.”

On Sun, May 11, 2014 at 1:43 AM, Arup R.