[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Static) Constructors/Destructors in Ruby

PrimaryKey

3/31/2006 6:25:00 PM

Hello!

After reading the Pickaxe book I noticed it provides almost no
information about object lifecycle and no mention of destructors. As a
Java/C# guy my gut reaction was: Wouldnâ??t it be nice to have a
â??unintializeâ? method to be called when the object is being destroyed?

There seems to be some form of destructor (called finalizers) in the
ObjectSpace module, but it seems to be an afterthought rather that an
integral feature of the language.

Another thing I find very surprising for a language having such a rich
OO model is the lack of static constructors (available in C# and somehow
in Java) and static destructors (something I always wanted to have in
Java/C#).

Based on that I will appreciate your thoughts on why a language having
such advanced OO features is lacking in these areas and is there any way
to emulate these.

Thanks

--
Posted via http://www.ruby-....


26 Answers

Marcin Mielzynski

3/31/2006 6:38:00 PM

0

PrimaryKey wrote:
> Hello!
>
> After reading the Pickaxe book I noticed it provides almost no
> information about object lifecycle and no mention of destructors. As a
> Java/C# guy my gut reaction was: Wouldnâ??t it be nice to have a
> â??unintializeâ? method to be called when the object is being destroyed?
>

Java does not have destructors either. finalize method is not guaranteed
to be called


> Another thing I find very surprising for a language having such a rich
> OO model is the lack of static constructors (available in C# and somehow
> in Java) and static destructors (something I always wanted to have in
> Java/C#).

Ruby has them. Every class definition is an executable code (public,
private, protected are methods) so you can alternate method definitions
with arbitrary code (in fact, this is much more powerful mechanism than
static blocks in Java and static constructors in C#)

lopex

Tim Hunter

3/31/2006 6:52:00 PM

0

PrimaryKey wrote:
> Hello!
>
> After reading the Pickaxe book I noticed it provides almost no
> information about object lifecycle and no mention of destructors. As a
> Java/C# guy my gut reaction was: Wouldn't it be nice to have a
> "unintialize" method to be called when the object is being destroyed?
>
> There seems to be some form of destructor (called finalizers) in the
> ObjectSpace module, but it seems to be an afterthought rather that an
> integral feature of the language.

Not so much an afterthought as discouraged.

Here's a good discussion about Ruby's finalizers:
http://www.rubygarden.org/ruby?R...

Daniel Nugent

3/31/2006 6:52:00 PM

0

Does anyone know under what conditions the finalizer won't be called?On 3/31/06, Marcin Mielzynski <lopexx@autograf.pl> wrote:> PrimaryKey wrote:> > Hello!> >> > After reading the Pickaxe book I noticed it provides almost no> > information about object lifecycle and no mention of destructors. As a> > Java/C# guy my gut reaction was: Wouldn't it be nice to have a> > "unintialize" method to be called when the object is being destroyed?> >>> Java does not have destructors either. finalize method is not guaranteed> to be called>>> > Another thing I find very surprising for a language having such a rich> > OO model is the lack of static constructors (available in C# and somehow> > in Java) and static destructors (something I always wanted to have in> > Java/C#).>> Ruby has them. Every class definition is an executable code (public,> private, protected are methods) so you can alternate method definitions> with arbitrary code (in fact, this is much more powerful mechanism than> static blocks in Java and static constructors in C#)>> lopex>>---Dan NugentDon't Feel Like Typing? Send me a voicemail:http://odeo.com/sendmeamessage...

Jim Weirich

3/31/2006 7:21:00 PM

0

PrimaryKey wrote:
> There seems to be some form of destructor (called finalizers) in the
> ObjectSpace module, but it seems to be an afterthought rather that an
> integral feature of the language.

Actually, it is a very well thought out feature of the language. In
Java, the finalizer is run when the object is eligible for garbage
collection. But within the finalizer, you can create a new reference to
the object and make it ineligible for collection.

In Ruby, the finalizer is not run until *after* the object is collected.
Since the object itself is no longer around, there is no possibility of
even accidently creating a new reference to the object. It also means
that the finalizer can't be an instance method (because the instance is
gone when the finalizer is run).

> Another thing I find very surprising for a language having such a rich
> OO model is the lack of static constructors (available in C# and somehow
> in Java) and static destructors (something I always wanted to have in
> Java/C#).

How would you use static constructors/destructors? Perhaps we can show
you Ruby equivalents.

--
-- Jim Weirich

--
Posted via http://www.ruby-....


PrimaryKey

3/31/2006 7:59:00 PM

0

> How would you use static constructors/destructors? Perhaps we can show
> you Ruby equivalents.
>
> --
> -- Jim Weirich

Please consider the following (pseudo) C# example:

public class AS400Server
{
static AS400 server;

static AS400Server()
{
server = new AS400Connection("SERVERNAME");
}

static ~AS400Server() // This is not real C#
{
server.disconnect();
}

static int GetServer()
{
return server;
}
}

I know this probably can be emulated using a singleton, I still believe
having static destructors will be nice because:

1. I like the constructor/destructor symmetry in C++
2. It can be helpful for meta-programming purposes. My impression is
most languages try to implement the traditional OO tools on class
(static) level

Thanks

--
Posted via http://www.ruby-....


Logan Capaldo

3/31/2006 9:09:00 PM

0


On Mar 31, 2006, at 2:58 PM, PrimaryKey wrote:

>> How would you use static constructors/destructors? Perhaps we can
>> show
>> you Ruby equivalents.
>>
>> --
>> -- Jim Weirich
>
> Please consider the following (pseudo) C# example:
>
> public class AS400Server
> {
> static AS400 server;
>
> static AS400Server()
> {
> server = new AS400Connection("SERVERNAME");
> }
>
> static ~AS400Server() // This is not real C#
> {
> server.disconnect();
> }
>
> static int GetServer()
> {
> return server;
> }
> }
>
> I know this probably can be emulated using a singleton, I still
> believe
> having static destructors will be nice because:
>
> 1. I like the constructor/destructor symmetry in C++
> 2. It can be helpful for meta-programming purposes. My impression is
> most languages try to implement the traditional OO tools on class
> (static) level
>
> Thanks
>
> --
> Posted via http://www.ruby-....
>

class AS400Server
@server = AS400Connection.new
def self.get_server
@server
end
end

As for a static deconstructor, can't really help you, I don't think
classes are really ever GCed until the very end, anyway. however, you
could do this:

class AS400Server
at_exit { @server.disconnect }
end



Justin Collins

3/31/2006 9:32:00 PM

0

I believe this is the case if the garbage collector never runs. That is,
if the program exits before it is necessary.
Also, there are situations in which objects are never released, so the
finalize method won't be called then, either. If I recall correctly.

-Justin

Daniel Nugent wrote:
> Does anyone know under what conditions the finalizer won't be called?
>
> On 3/31/06, Marcin Mielzynski <lopexx@autograf.pl> wrote:
>
>> PrimaryKey wrote:
>>
>>> Hello!
>>>
>>> After reading the Pickaxe book I noticed it provides almost no
>>> information about object lifecycle and no mention of destructors. As a
>>> Java/C# guy my gut reaction was: Wouldn't it be nice to have a
>>> "unintialize" method to be called when the object is being destroyed?
>>>
>>>
>> Java does not have destructors either. finalize method is not guaranteed
>> to be called
>>
>>
>>
>>> Another thing I find very surprising for a language having such a rich
>>> OO model is the lack of static constructors (available in C# and somehow
>>> in Java) and static destructors (something I always wanted to have in
>>> Java/C#).
>>>
>> Ruby has them. Every class definition is an executable code (public,
>> private, protected are methods) so you can alternate method definitions
>> with arbitrary code (in fact, this is much more powerful mechanism than
>> static blocks in Java and static constructors in C#)
>>
>> lopex
>>
>>
>>
>
>
> --
> -Dan Nugent
>
> Don't Feel Like Typing? Send me a voicemail:
> http://odeo.com/sendmeamessage...
>

Joel VanderWerf

3/31/2006 9:55:00 PM

0

PrimaryKey wrote:
>> How would you use static constructors/destructors? Perhaps we can show
>> you Ruby equivalents.
>>
>> --
>> -- Jim Weirich
>
> Please consider the following (pseudo) C# example:
>
> public class AS400Server
> {
> static AS400 server;
>
> static AS400Server()
> {
> server = new AS400Connection("SERVERNAME");
> }
>
> static ~AS400Server() // This is not real C#
> {
> server.disconnect();
> }
>
> static int GetServer()
> {
> return server;
> }
> }

I would ask first: does having one class per connection ("SERVERAME")
scale well? what if you need two servers...? This question isn't really
off topic, because once you accept this possibility, it becomes very
natural in ruby to do this:

class AS400Server
def initialize(serv_name)
@serv_name = serv_name
end

def connect
# do something to connect to the server
yield self
ensure
# do something to disconnet
end
end

Then in your main code you might have:

def run_my_app
AS400Server.new("SERVERNAME").connect do |serv|
# do something with serv
end
end

or

THE_SERVER = AS400Server.new("SERVERNAME")
def run_my_app
THE_SERVER.connect do
# do something with THE_SERVER
end
end

Unless you exit with Kernel#exit! or the process is killed in a way that
ruby cannot catch (e.g., SIGKILL), the ensure clause will close the
server connection first. (Anyway, in the case of SIGKILL, a static
destructor would not be called either, right? Or can C# do that?)

(You can even roll the #connect method into initialize, if you want.
Passing the server object from yeild isn't really necessary, but you may
need to reference it in the block.)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Marcin Mielzynski

4/1/2006 1:09:00 AM

0

Jim Weirich wrote:

>
> In Ruby, the finalizer is not run until *after* the object is collected.
> Since the object itself is no longer around, there is no possibility of
> even accidently creating a new reference to the object. It also means
> that the finalizer can't be an instance method (because the instance is
> gone when the finalizer is run).


This makes perfect sense since Ruby uses mark&sweep. But in Python is it
possible to supply __del__ method which seems to be an instance one
(correct me if I'm wrong), but the moment of GC cannot be determined
anyways. And as rmagick mentioned, explicit destrutors are not needed
anyways.

lopex

Jim Weirich

4/1/2006 3:27:00 AM

0

PrimaryKey wrote:
> Please consider the following (pseudo) C# example:

I think Logan Capaldo's answer is hits pretty close to your intention
here. However, I have to comment because I've been retrofitting unit
tests in to a legacy (Java) code base this past week and I've come to
*hate* static initializers.

The code base does something similar to your example and the code is
impossible to unit test without some restructuring. Since the database
connection is made in static initializers, I can't even load the class
without having the infrastructure for a database connection available.
Yuck!

I've come to consider complex static initializers (e.g. making DB
connections in the initilizer) a thing of "evil".

Ok, rant over. ;)

> [...]
> 2. It can be helpful for meta-programming purposes. My impression is
> most languages try to implement the traditional OO tools on class
> (static) level

I'm not getting statement #2 above.

Thanks.

--
-- Jim Weirich

--
Posted via http://www.ruby-....