[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

new vs. initialize

Jeff Davis

1/31/2005 4:50:00 AM

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

Thanks,
Jeff Davis


4 Answers

Mark Hubbart

1/31/2005 5:01:00 AM

0

Hi,

On Mon, 31 Jan 2005 13:49:43 +0900, Jeff Davis <jdavis-list@empires.org> 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


Eric Hodel

1/31/2005 5:02:00 AM

0

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 - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

PL

9/28/2009 6:43:00 PM

0

Albert wrote:
> Video extra: ?Qu? son los Comit?s de Defensa de la Revoluci?n (CDRs)?

CUBA - CDR - BRR - CONTINGENTES
http://www.youtube.com/watch?v=b...

CUBA Los CDR Comites de Defensa de la Revolucion
http://www.youtube.com/watch?v=r...

Mas:
http://www.cubaverdad.n...

PL

PL

9/28/2009 7:58:00 PM

0

Albert wrote:
> "PL" <pl.nospam@pandora.be> wrote in message
news:fs7wm.221137$LX3.155810@newsfe17.ams2...
>> Albert wrote:
>>> Video extra: ??Qu?? son los Comit??s de Defensa de la Revoluci??n (CDRs)?
>>> http://www.youtube.com/watch?v=P...
>
>> CUBA - CDR - BRR - CONTINGENTES
>> http://www.youtube.com/watch?v=b...
>
> PL, ??a ti te pagan por mentira o qu???

No y no digo mentiras.
Pero a ti Albert, te pagan?
Me parece que para ti es normal de ser pagado por tus mentiras.

> ??Has pensado de qu?? vas a vivir si se cae la Revoluci??n Cubana?

Cuba cada d??a es mas cerca de la libertad.

> ??A qui??n vas a vender tus mentiras?

No "vendo mentiras" como tu.
Regalo verdades.

Como siempre: el hecho que necesitas insultos y mentiras muestras que
puedes negar nada.

Lo que corto Albert por miedo que la gente lo podr??an ver:

CUBA - CDR - BRR - CONTINGENTES
http://www.youtube.com/watch?v=b...

CUBA Los CDR Comites de Defensa de la Revolucion
http://www.youtube.com/watch?v=r...

Mas:
http://www.cubaverdad.n...

PL