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

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

···

On 8 Mar 2007, at 11:40, Gregory Brown wrote:

On 3/8/07, Andrew Stewart <boss@airbladesoftware.com> wrote:

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

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-bildarchiv.de/

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

Thank you!
Andy

···

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

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!

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

···

On 8 Mar 2007, at 12:17, jens wille wrote:

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 :wink:

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-bildarchiv.de/

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

James Edward Gr*a*y II

···

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

Yes, remembre, one's a color, the other's a colo*u*r

···

On 3/8/07, James Edward Gray II <james@grayproductions.net> wrote:

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

James,

James Edward Gr*a*y II

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 :wink:

Cheers
Rbroet

···

On 3/9/07, Andrew Stewart <boss@airbladesoftware.com> wrote:

James,

> James Edward Gr*a*y II

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

Regards,
Andy Stewart

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

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

James Edward Gray II

···

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

On 3/9/07, Andrew Stewart <boss@airbladesoftware.com> wrote:

James,

> James Edward Gr*a*y II

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 :wink:

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

···

On 9 Mar 2007, at 12:32, Robert Dober wrote: