From: Eric Hodel Date: 2005-01-31T14:02:12+09:00 Subject: Re: new vs. initialize --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 # => # -- Eric Hodel - drbrain@segment7.net - 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--