How To Find The Name Of A Variable

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 S.

On 3/8/07, Andrew S. [email protected] 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”]

On 8 Mar 2007, at 11:40, Gregory B. wrote:

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 S.

hi andy!

Pit C. [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

Andrew S. schrieb:

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

On 8 Mar 2007, at 12:09, Pit C. wrote:

@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

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

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

Andrew S. [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 :wink:

cheers
jens

Yes, remembre, one’s a color, the other’s a colour

On Mar 8, 2007, at 6:49 AM, Andrew S. 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 Gray II

On 3/9/07, Andrew S. [email protected] wrote:

James,

James Edward Gray II

Oops! Many apologies – can’t believe I misspelled your surname.

Regards,
Andy S.

James it seems that you should change name, maybe John D.?
Gosh we really treat you badly and if I remember correctly I was one
of the worst culprits!
Well just sayed that so that Andy Stuart feels better :wink:

Cheers
Rbroet

James,

James Edward Gray II

Oops! Many apologies – can’t believe I misspelled your surname.

Regards,
Andy S.

On Mar 9, 2007, at 6:32 AM, Robert D. wrote:

James it seems that you should change name, maybe John D.?
Gosh we really treat you badly and if I remember correctly I was one
of the worst culprits!
Well just sayed that so that Andy Stuart feels better :wink:

Don’t worry, my own family sometimes misspells my name. :wink:

James Edward G. II

On 9 Mar 2007, at 12:32, Robert D. wrote:

Well just sayed that so that Andy Stuart feels better :wink:

Touché! Being spelled ‘Stuart’ does vex me :slight_smile: Happily my family
tends to spell my name correctly…

Cheers
Rbroet

Yours,
Adny