Forum: Ruby question about initialize

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ce5990549a8185ad45643be2cfbff9b5?d=identicon&s=25 Mike Steiner (Guest)
on 2007-06-12 19:35
(Received via mailing list)
Why is the constructor method named initialize? Why not name it new to
be
consistent?

Mike Steiner
9f026ca1d86871f937ef6f3627bf527a?d=identicon&s=25 Zachary Holt (Guest)
on 2007-06-12 19:49
(Received via mailing list)
On Jun 12, 2007, at 10:34 AM, Mike Steiner wrote:

> Why is the constructor method named initialize? Why not name it new
> to be
> consistent?
Typically, :new is a class method, which calls the instantiated
object's :initialize instance method, if one is available, with the
parameters passed to :new.
You can override the class method :new (this is a good place to use
super), but be sure to return the newly minted object.  Often you can
just override the :initialize instance method, however.

If there's no :new class method in your class definition, then you're
getting Object.new (or some other super class's :new).
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-06-12 19:55
(Received via mailing list)
Mike Steiner wrote:
> Why is the constructor method named initialize? Why not name it new
> to be consistent?

I had this same question when I first started out. The answer I came
to realize is that "new" is a class method that allocates the object,
and "initialize" is the instance method that does the per-object setup
you want.

For more details on how I came to realize this, read the "new versus
initialize" section of this post:
Short URL: http://rubyurl.com/n1Q
Full URL:
http://groups.google.com/group/comp.lang.ruby/tree...
Ce5990549a8185ad45643be2cfbff9b5?d=identicon&s=25 Mike Steiner (Guest)
on 2007-06-12 23:17
(Received via mailing list)
Thanks! That link makes it much clearer.

Mike Steiner
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2008-11-21 00:50
>> Why is the constructor method named initialize? Why not name it new
>> to be
>> consistent?
> Typically, :new is a class method, which calls the instantiated
> object's :initialize instance method, if one is available, with the
> parameters passed to :new.
> You can override the class method :new (this is a good place to use
> super), but be sure to return the newly minted object.  Often you can
> just override the :initialize instance method, however.
>
> If there's no :new class method in your class definition, then you're
> getting Object.new (or some other super class's :new).

Would it be better to have initialize called 'on_new' so that the
connection is more firm?
Thoughts?
-=R
Fa2521c6539342333de9f42502657e5a?d=identicon&s=25 Eleanor McHugh (Guest)
on 2008-11-21 22:01
(Received via mailing list)
On 20 Nov 2008, at 23:46, Roger Pack wrote:
>> If there's no :new class method in your class definition, then you're
>> getting Object.new (or some other super class's :new).
>
> Would it be better to have initialize called 'on_new' so that the
> connection is more firm?
> Thoughts?

Unfortunately :on_new assumes that :initialize is only ever called
from :new, which isn't always the case.


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
----
raise ArgumentError unless @reality.responds_to? :reason
This topic is locked and can not be replied to.