Forum: Ruby new/initialize

201ab62b10b1ce61759a091d3b307fa1?d=identicon&s=25 Tom Allison (Guest)
on 2006-04-10 20:09
(Received via mailing list)
Just a point of clarification.

When I call
Obj.new()

I actually call the
clall Obj
  def initialize()
  end
end

so there is an implied connection between new() and initialize()?

Historically is there some reason why we didn't just stick with the
'def new()'
or did I miss something.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-04-10 20:18
(Received via mailing list)
On Apr 10, 2006, at 11:07 AM, Tom Allison wrote:

> When I call
> Obj.new()
>
> I actually call the
> clall Obj
>   def initialize()
>   end
> end

Not exactly.  You call Object::new.  Object::new calls initialize
after allocating a new instance.

> so there is an implied connection between new() and initialize()?

There is an explicit connection between Object::new and
Object#initialize

Object::new is implemented something like this, but in C:

class Object
   def self.new(*args, &block)
     obj = allocate
     obj.send(:initialize, *args, &block)
     return obj
   end
end

> Historically is there some reason why we didn't just stick with the
> 'def new()'
> or did I miss something.

That would be an instance method.  Object::new is a class method.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-04-10 20:18
(Received via mailing list)
On 4/10/06, Tom Allison <tallison@tacocat.net> wrote:
> end
>
> so there is an implied connection between new() and initialize()?
>
> Historically is there some reason why we didn't just stick with the
> 'def new()'
> or did I miss something.

"new" allocates space for the new object and creates it. It then calls
"initialize" to initialize the newly created object. Typically that's
that only part you want to customize which is why you write an
"initialize" method instead of a "new" method.
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-04-10 20:21
(Received via mailing list)
On 4/10/06, Tom Allison <tallison@tacocat.net> wrote:
>
> so there is an implied connection between new() and initialize()?
>
> Historically is there some reason why we didn't just stick with the
> 'def new()'
> or did I miss something.

No, you actually call Obj::new. This is a method on Obj; this method
looks something like this, although it's written in C:

  class Obj
    class << self
      def new(*args, &block)
        obj = self.allocate
        obj.__send__(:initialize, *args, &block)
        obj
      end
    end
  end

-austin
201ab62b10b1ce61759a091d3b307fa1?d=identicon&s=25 Tom Allison (Guest)
on 2006-04-10 20:21
(Received via mailing list)
>That would be an instance method.  Object::new is a class method.

Ding!
Light went on.  Thanks.
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-04-10 20:21
(Received via mailing list)
new() is actually just  a class method of the class object (Obj). You
can create your own method if need be:

class Obj
  def self.new
    super
    ...
  end
end

Calling 'super' is pretty important otherwise your object is not
properly instantiated (I think). You can also wind up with the wrong
kind of object if you're not careful

irb(main):001:0> class Obj
irb(main):002:1>   def self.new
irb(main):003:2>     return "Hello!"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> o = Obj.new
=> "Hello!"
irb(main):008:0> o.class
=> String


Farrel
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.