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

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-05-10 20:48
(Received via mailing list)
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
(http://www.ruby-doc.org/core-2.1.1/Array.html#method-c-new), *::new* is
defined(overrode) inside the class *Array*. Then why still it is showing
the *owner* as _Class_ ?


Regards,
Arup Rakshit
D9ebdcb66f1583378e6f72155db507e2?d=identicon&s=25 Hans Mackowiak (hanmac)
on 2014-05-10 22:00
#initialize is in Rdoc as #new documented.
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2014-05-11 01:01
(Received via mailing list)
On May 10, 2014, at 2:45 PM, Arup Rakshit <aruprakshit@rocketmail.com>
wrote:

>     14
>
> As per the doc (http://www.ruby-doc.org/core-2.1.1/Array.html#method-c-new),
*::new* is defined(overrode) inside the class *Array*. Then why still it is
showing the *owner* as _Class_ ?
>
> Regards,
> Arup Rakshit


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 Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-05-11 06:46
(Received via mailing list)
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 Stok <mike@stok.ca> wrote:



On May 10, 2014, at 2:45 PM, Arup Rakshit <aruprakshit@rocketmail.com>
wrote:

Code# 1 :
> 14
>Code# 2:
>
>
>Array.method(:new).owner # => Class
>
>
>As per the doc (http://www.ruby-doc.org/core-2.1.1/Array.html#method-c-new),
*::new* is defined(overrode) inside the class *Array*. Then why still it is
showing the *owner* as _Class_ ?
>
>
>Regards,
>Arup Rakshit
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 Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
D9ebdcb66f1583378e6f72155db507e2?d=identicon&s=25 Hans Mackowiak (hanmac)
on 2014-05-11 08:59
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
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-05-11 16:35
Hans Mackowiak 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
D9ebdcb66f1583378e6f72155db507e2?d=identicon&s=25 Hans Mackowiak (hanmac)
on 2014-05-11 16:54
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2014-05-12 02:33
(Received via mailing list)
[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 http://www.ruby-doc.org/core-2.1.1/Class.html#method-i-new
"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 Rakshit
15a0a233d3bf86fea7bbaa525f33897d?d=identicon&s=25 Rajeev N B (Guest)
on 2014-05-12 07:20
(Received via mailing list)
+1 for Abinoam’s answer.

Thanks
--
Rajeev N B
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-05-12 17:00
(Received via mailing list)
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.
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2014-05-13 04:18
(Received via mailing list)
You're welcome! :-)

On Mon, May 12, 2014 at 11:00 AM, Arup Rakshit
<aruprakshit@rocketmail.com> wrote:

>> Did it clarified the issue?
>
> Yes it is.
>
> Thanks for such detail explanation.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.