[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How To Find The Name Of A Variable

Andrew Stewart

3/8/2007 11:22:00 AM

Hello,

I'm wondering how I can ask a variable what its name is, so that I
can convert it into a symbol.

For example, I'd like to convert @bar to :bar.

Thanks and regards,
Andy Stewart

13 Answers

Gregory Brown

3/8/2007 11:40:00 AM

0

On 3/8/07, Andrew Stewart <boss@airbladesoftware.com> wrote:
> Hello,
>
> I'm wondering how I can ask a variable what its name is, so that I
> can convert it into a symbol.
>
> For example, I'd like to convert @bar to :bar.

You can't. Variables aren't objects in Ruby. However, if you post a
bit of context of the actual problem, I bet folks here will be able to
help you solve it.

You *can* get the listing of variables though:
>> @foo = 10
=> 10
>> instance_variables
=> ["@foo"]
>> a = 3
=> 3
>> local_variables
=> ["_", "__", "a"]

Andrew Stewart

3/8/2007 11:57:00 AM

0


On 8 Mar 2007, at 11:40, Gregory Brown wrote:
> On 3/8/07, Andrew Stewart <boss@airbladesoftware.com> wrote:
>> Hello,
>>
>> I'm wondering how I can ask a variable what its name is, so that I
>> can convert it into a symbol.
>>
>> For example, I'd like to convert @bar to :bar.
>
> You can't. Variables aren't objects in Ruby. However, if you post a
> bit of context of the actual problem, I bet folks here will be able to
> help you solve it.

Aha! That explains why I couldn't. Here's the context:

I have a line item object in memory (@line_item) and a set of line
item attributes in a hash (keyed under params[:line_item]; this is in
a Rails controller). I only want to bother processing the line item
hash if the quantity of either the in-memory object or the one in the
hash is non-zero.

So I wrote this method:

# TODO: the only reason for passing the symbol is that I don't know
# how to convert the line_item's variable name into a symbol.
def line_item_relevant(line_item, symbol)
# Either we have an existing line item with non-zero quantity
(line_item && line_item.quantity > 0) ||
# Or we hearing about a line item with non-zero quantity
(params[symbol] && params[symbol][:quantity] > 0)
end

And I call it like this:

... if line_item_relevant @line_item_jacket, :line_item_jacket

And like this:

... if line_item_relevant @line_item_shirt, :line_item_shirt

Et cetera.

It would be less repetitive if I could ditch the symbol argument to
the method. Hence my original question.

Any ideas would be welcome!

> You *can* get the listing of variables though:
>>> @foo = 10
> => 10
>>> instance_variables
> => ["@foo"]
>>> a = 3
> => 3
>>> local_variables
> => ["_", "__", "a"]

That's useful to know, thanks.

Regards,
Andy Stewart

Pit Capitain

3/8/2007 12:10:00 PM

0

Andrew Stewart schrieb:
> (...)
> And I call it like this:
>
> ... if line_item_relevant @line_item_jacket, :line_item_jacket
>
> And like this:
>
> ... if line_item_relevant @line_item_shirt, :line_item_shirt
>
> Et cetera.
>
> It would be less repetitive if I could ditch the symbol argument to the
> method. Hence my original question.

Andy, you could try to change the way you create your line item objects.
Instead of storing them in separate instance variables, you could store
them in a Hash. Instead of

@line_item_jacket = create_line_item_jacket
@line_item_shirt = create_line_item_shirt

you could do something like

@line_items = {}
@line_items[ :line_item_jacket ] = create_line_item_jacket
@line_items[ :line_item_shirt ] = create_line_item_shirt

then you can just do

if line_item_relevant :line_item_shirt

Regards,
Pit

Jens Wille

3/8/2007 12:17:00 PM

0

hi andy!

Pit Capitain [08/03/07 13:09]:
> then you can just do
>
> if line_item_relevant :line_item_shirt
or you could use that method call to retrieve your instance variable:

line_item = instance_variable_get(:"@#{symbol}")

cheers
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
An St. Laurentius 4, 50931 Köln
Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de
http://www.prometheus-bild...

Andrew Stewart

3/8/2007 12:18:00 PM

0


On 8 Mar 2007, at 12:09, Pit Capitain wrote:
> Andy, you could try to change the way you create your line item
> objects. Instead of storing them in separate instance variables,
> you could store them in a Hash. Instead of
>
> @line_item_jacket = create_line_item_jacket
> @line_item_shirt = create_line_item_shirt
>
> you could do something like
>
> @line_items = {}
> @line_items[ :line_item_jacket ] = create_line_item_jacket
> @line_items[ :line_item_shirt ] = create_line_item_shirt
>
> then you can just do
>
> if line_item_relevant :line_item_shirt

Pit, that's a neat approach. I'll try it.

Thank you!
Andy

Andrew Stewart

3/8/2007 12:20:00 PM

0

Hi Jens,

On 8 Mar 2007, at 12:17, jens wille wrote:
> itain [08/03/07 13:09]:
>> then you can just do
>>
>> if line_item_relevant :line_item_shirt
> or you could use that method call to retrieve your instance variable:
>
> line_item = instance_variable_get(:"@#{symbol}")

Aha! I like that too!

Thanks for the suggestion,
Andy

Andrew Stewart

3/8/2007 12:49:00 PM

0

Hi Jens!

On 8 Mar 2007, at 12:17, jens wille wrote:
> or you could use that method call to retrieve your instance variable:
>
> line_item = instance_variable_get(:"@#{symbol}")

This works perfectly for me (omitting the colon) with minimal change
to my code.

I was trying to go from @foo to :foo. It never crossed my mind to
try the reverse, i.e. from :foo to @foo.

I suppose that's an example of James Edward Grey II's favourite
dictum, "If all else fails, reverse the data."

Thanks again!
Andy

Jens Wille

3/8/2007 12:58:00 PM

0

Andrew Stewart [08/03/07 13:49]:
> This works perfectly for me (omitting the colon) with minimal
> change to my code.
great!

> I was trying to go from @foo to :foo. It never crossed my mind
> to try the reverse, i.e. from :foo to @foo.
>
> I suppose that's an example of James Edward Grey II's favourite
> dictum, "If all else fails, reverse the data."
*lol* maybe that's why this idea came to my mind ;-)

cheers
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
An St. Laurentius 4, 50931 Köln
Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de
http://www.prometheus-bild...

James Gray

3/8/2007 1:42:00 PM

0

On Mar 8, 2007, at 6:49 AM, Andrew Stewart wrote:

> I suppose that's an example of James Edward Grey II's favourite
> dictum, "If all else fails, reverse the data."

I'm telling you it works! (I have no idea why...)

James Edward Gr*a*y II


Andrew Stewart

3/9/2007 12:25:00 PM

0

James,

> James Edward Gr*a*y II

Oops! Many apologies -- can't believe I misspelled your surname.

Regards,
Andy Stewart