[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Accesing the name of an instance from within an method

Gerald Ebberink

1/31/2007 10:36:00 AM


Well here comes the big picture. I have a class contains some data and
amongst other things can generate graphs. What I would like to do is to
place these graphs in a bunch of files which have unique names (otherwise I
end up with always the last graph). Since only the variable (and the data)
are different amongst the instances I wonder how I could give them meaning
full names like "20070131 var1 XY.jpg" "20070131 var1 Both.jpg " etc.

Now I did think about giving the var as an option while creating the
instance, but that doesn't help since it is perfectly possible in the code
that variable from which the instance is referenced is changed.

Now I come to think of it I could try to overload the = operater to let some
internal variable be changed when the reference is changed but then I still
need the name of the variable.

Kind regards
Gerald Ebberink


-----Original Message-----
From: gwtmp01@mac.com [mailto:gwtmp01@mac.com]
Sent: dinsdag 30 januari 2007 17:51
To: ruby-talk ML
Subject: Re: Accesing the name of an instance from within an method


On Jan 30, 2007, at 2:13 AM, Gerald Ebberink wrote:
> I am wondering if it is possible to access the name of an instance
> from within a method.
I'm going to invoke the "What are you trying to do?" rule.

If you give this group the bigger picture of what you are trying to
accomplish
I'm sure we'll give you any number of options other than "you can't
do that in Ruby".

Gary Wright







2 Answers

burn.redmond.burn

1/31/2007 11:49:00 AM

0

On Jan 31, 11:35 am, "Gerald Ebberink" <g.h.p.ebber...@nclr.nl> wrote:
>
> Now I come to think of it I could try to overload the = operater to let some
> internal variable be changed when the reference is changed but then I still
> need the name of the variable.

You can't overload the assignment operator. It's not a method. See
page 324 of the pickaxe.

Tim Pease

1/31/2007 9:31:00 PM

0

On 1/31/07, Gerald Ebberink <g.h.p.ebberink@nclr.nl> wrote:
>
> Well here comes the big picture. I have a class contains some data and
> amongst other things can generate graphs. What I would like to do is to
> place these graphs in a bunch of files which have unique names (otherwise I
> end up with always the last graph). Since only the variable (and the data)
> are different amongst the instances I wonder how I could give them meaning
> full names like "20070131 var1 XY.jpg" "20070131 var1 Both.jpg " etc.
>
> Now I did think about giving the var as an option while creating the
> instance, but that doesn't help since it is perfectly possible in the code
> that variable from which the instance is referenced is changed.
>
> Now I come to think of it I could try to overload the = operater to let some
> internal variable be changed when the reference is changed but then I still
> need the name of the variable.
>

When you create an instance of this class that contains all the data,
give it a base filename to use when creating and saving off graphs.
Use an incrementing counter to make sure all the filenames are unique.

If you want to get fancy, you can look at the directory where the file
is to be created ...

File.dirname('path/to/data/filename.graph') #=> 'path/to/data'

and see if there are already files named 'filename.1.graph' etc. That
will give you the number to start using to make sure you don't
overwrite older graphs from previous runs.

Just my thoughts on that one :)

Blessings,
TwP