[#4567] Re: What's the biggest Ruby development? — Aleksi Niemel<aleksi.niemela@...>

Dave said:

18 messages 2000/08/23
[#4568] Q's on Marshal — Robert Feldt <feldt@...> 2000/08/23

[#4580] RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/25

[#4584] Re: RubyUnit testcase run for different init params? — Dave Thomas <Dave@...> 2000/08/25

Robert Feldt <feldt@ce.chalmers.se> writes:

[#4623] Re: RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/28

On Sat, 26 Aug 2000, Dave Thomas wrote:

[#4652] Andy and Dave's European Tour 2000 — Dave Thomas <Dave@...>

24 messages 2000/08/30
[#4653] Re: Andy and Dave's European Tour 2000 — matz@... (Yukihiro Matsumoto) 2000/08/30

Hi,

[#4657] Ruby tutorials for newbie — Kevin Liang <kevin@...> 2000/08/30

Hi,

[ruby-talk:4707] Re: new, initialize, etc.

From: Dave Thomas <Dave@...>
Date: 2000-08-31 21:58:48 UTC
List: ruby-talk #4707
hal9000@hypermetrics.com writes:

> As I see it, new is typically a class method which invokes the
> initialize method.
> 
> First of all, why? I guess I can understand new being a class method,
> but why is initialize named differently? Why not just define new for
> the class and put the initializing code in there?

There's something magical that happens between 'new' and
'initialize'. When 'new' is called, it is being run in the context of
the class that you are creating: there is no instance of that class at 
this point. One of new's jobs is to create that instance. Here's the
magic. Once created, new invokes initialize, but in the context of
that instance.

In Ruby, you might implement this as something like:


   class Impossible

     def Impossible.new(*args, &block)
       obj = <allocate storage>
       setupDataStructuresIn(obj)
       obj.initialize(*args, &block)
     end

     def initialize(....)

So, object creation is a two-step process.

> Second, I've read a little about finalizing objects; I'm not sure why
> or when to do it. And why do it that way, rather than just provide a
> method to be called by the GC when the object dies?

That's all that the finalizer is: a method called when an object is
reaped (love that word) by the garbage collector.


> Finally, how are BEGIN and END supposed to be used? I've read what they
> do -- it seems to me they would be useful for modules -- but I don't
> actually grasp them.

They're just blobs of code that are run to initialize stuff for a
class or module. For example, the 'profile' module uses an END block
to dump out the profiling statistics at the end of the program's
execution.

They aren't used that often.


> Are they completely unrelated to the concept of creating and
> destroying objects?

Pretty much.


Regards


Dave

In This Thread