How To Find The Name Of A Variable

A

Andrew Stewart

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
 
G

Gregory Brown

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"]
 
A

Andrew Stewart

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
 
P

Pit Capitain

Andrew said:
(...)
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
 
J

jens wille

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: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 
A

Andrew Stewart

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
 
A

Andrew Stewart

Hi Jens,

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
 
A

Andrew Stewart

Hi Jens!

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
 
J

jens wille

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: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 
J

James Edward Gray II

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
 
R

Robert Dober

James,


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

Regards,
Andy Stewart
James it seems that you should change name, maybe John Doe?
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 ;)

Cheers
Rbroet
 
J

James Edward Gray II

James it seems that you should change name, maybe John Doe?
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 ;)

Don't worry, my own family sometimes misspells my name. ;)

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,237
Messages
2,571,189
Members
47,824
Latest member
MckinleyBu

Latest Threads

Top