new vs. initialize

Discussion in 'Ruby' started by Jeff Davis, Jan 31, 2005.

  1. Jeff Davis

    Jeff Davis Guest

    If MyClass.new calls the initialize method, what calls the new method?
    What if both are defined?

    Thanks,
    Jeff Davis
     
    Jeff Davis, Jan 31, 2005
    #1
    1. Advertising

  2. Jeff Davis

    Mark Hubbart Guest

    Hi,

    On Mon, 31 Jan 2005 13:49:43 +0900, Jeff Davis <> wrote:
    > If MyClass.new calls the initialize method, what calls the new method?
    > What if both are defined?


    *You* call the new method. It's pre-defined in Class to work something
    like this:

    class Class
    def new
    obj = self.allocate # allocates the object
    obj.initialize # calls the object's initialize method
    return obj # returns the object
    end
    end

    You can redefine new to do anything you want, remembering that people
    will expect it to return an object of that class.

    cheers,
    Mark
     
    Mark Hubbart, Jan 31, 2005
    #2
    1. Advertising

  3. Jeff Davis

    Eric Hodel Guest

    --Apple-Mail-23--8502834
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain; charset=US-ASCII; format=flowed

    On 30 Jan 2005, at 20:49, Jeff Davis wrote:

    > If MyClass.new calls the initialize method, what calls the new method?
    > What if both are defined?


    You call the new method on a Class when you want a new instance of that
    class.
    The class calls #initialize to set up the instance for you.

    Here's how the two tie together in Ruby, expressed as Ruby code.

    class MyClass
    def self.new(*args, &block) # class method
    puts "called MyClass.new"
    obj = allocate
    # #initialize is a private method, so we use send to invoke it.
    obj.send :initialize, *args, &block
    return obj
    end

    def initialize # instance method
    puts "called MyClass#initialize"
    end
    end

    p MyClass.new

    # => called MyClass.new
    # => called MyClass#initialize
    # => #<MyClass:0x13356c>


    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

    --Apple-Mail-23--8502834
    content-type: application/pgp-signature; x-mac-type=70674453;
    name=PGP.sig
    content-description: This is a digitally signed message part
    content-disposition: inline; filename=PGP.sig
    content-transfer-encoding: 7bit

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (Darwin)

    iD8DBQFB/bvEMypVHHlsnwQRAvdfAJwM+zwcowgBfY41TIYiBF759kc1VgCffV91
    ref1B33L3IVJgoXL14aJGG4=
    =NJfI
    -----END PGP SIGNATURE-----

    --Apple-Mail-23--8502834--
     
    Eric Hodel, Jan 31, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    347
    Thomas Tutone
    Apr 5, 2006
  2. Replies:
    0
    Views:
    274
  3. Replies:
    0
    Views:
    312
  4. Juha Nieminen
    Replies:
    19
    Views:
    741
    Juha Nieminen
    Sep 6, 2008
  5. Replies:
    2
    Views:
    499
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page