tags:

views:

3034

answers:

10

I'm not entirely sure if this is possible in Ruby, but hopefully there's an easy way to do this. I want to declare a variable and later find out the name of the variable. That is, for this simple snippet:

foo = ["goo", "baz"]

How can I get the name of the array (here, "foo") back? If it is indeed possible, does this work on any variable (e.g., scalars, hashes, etc.)?

Edit: Here's what I'm basically trying to do. I'm writing a SOAP server that wraps around a class with three important variables, and the validation code is essentially this:

  [foo, goo, bar].each { |param|
      if param.class != Array
        puts "param_name wasn't an Array. It was a/an #{param.class}"
        return "Error: param_name wasn't an Array"
      end
      }

My question is then: Can I replace the instances of 'param_name' with foo, goo, or bar? These objects are all Arrays, so the answers I've received so far don't seem to work (with the exception of re-engineering the whole thing ala dbr's answer)

A: 

I do not know of any way to get a local variable name. But, you can use the instance_variables method, this will return an array of all the instance variable names in the object.

Simple call:

object.instance_variables

or

self.instance_variables

to get an array of all instance variable names.

Josh Moore
Thanks, but that doesn't particularly help. The specific problem I'm having is that I have three variables I want to iterate through and if they aren't all a certain data type, I want to print out that they aren't and print the name of that variable.Thanks though!
Chris Bunch
Chris: can you rephrase your actual question above to reflect this use case? I'm still not quite sure what your goal is.
James A. Rosen
A: 

Building on joshmsmoore, something like this would probably do it:

# Returns the first instance variable whose value == x
# Returns nil if no name maps to the given value
def instance_variable_name_for(x)
  self.instance_variables.find do |var|
    x == self.instance_variable_get(var)
  end
end
James A. Rosen
A: 

There's Kernel::local_variables, but I'm not sure that this will work for a method's local vars, and I don't know that you can manipulate it in such a way as to do what you wish to acheive.

Brian Warshaw
A: 

OK, it DOES work in instance methods, too, and, based on your specific requirement (the one you put in the comment), you could do this:

local_variables.each do |var|
  puts var if (eval(var).class != Fixnum)
end

Just replace Fixnum with your specific type checking.

Brian Warshaw
The problem I'm having is when you do a 'puts var'. Doing a puts on an Array prints out the contents of the Array, not the name of the Array. My current code above is similar to yours (although I didn't use eval).
Chris Bunch
+6  A: 

It seems you are trying to solve a problem that has a far easier solution..

Why not just store the data in a hash? If you do..

data_container = {'foo' => ['goo', 'baz']}

..it is then utterly trivial to get the 'foo' name.

That said, you've not given any context to the problem, so there may be a reason you can't do this..

[edit] After clarification, I see the issue, but I don't think this is the problem.. With [foo, bar, bla], it's equivalent like saying ['content 1', 'content 2', 'etc']. The actual variables name is (or rather, should be) utterly irrelevant. If the name of the variable is important, that is exactly why hashes exist.

The problem isn't with iterating over [foo, bar] etc, it's the fundamental problem with how the SOAP server is returing the data, and/or how you're trying to use it.

The solution, I would say, is to either make the SOAP server return hashes, or, since you know there is always going to be three elements, can you not do something like..

{"foo" => foo, "goo" => goo, "bar"=>bar}.each do |param_name, param|
      if param.class != Array
        puts "#{param_name} wasn't an Array. It was a/an #{param.class}"
        puts "Error: #{param_name} wasn't an Array"
      end
end
dbr
This is possible, but now the amount of repeating myself in my code I'm doing loses any benefit and I'd just as well break up my code into N segments, one for each array (and then hardcode the array's name)
Chris Bunch
+2  A: 

You can't, you need to go back to the drawing board and re-engineer your solution.

pauliephonic
That's what I was afraid of. I was just hoping that since I'm new to Ruby that there would possibly be some simple method name I just hadn't heard of yet.
Chris Bunch
+5  A: 

You need to re-architect your solution. Even if you could do it (you can't), the question simply doesn't have a sensible answer.

Imagine a get_name method.

a = 1
get_name(a)

Everyone could probably agree this should return 'a'

b = a
get_name(b)

Should it return 'b', or 'a', or an array containing both?

[b,a].each do |arg|
  get_name(arg)
end

Should it return 'arg', 'b', or 'a' ?

def do_stuff( arg )
  get_name(arg)
do
do_stuff(b)

Should it return 'arg', 'b', or 'a', or maybe the array of all of them? Even if it did return an array, what would the order be and how would I know how to interpret the results?

The answer to all of the questions above is "It depends on the particular thing I want at the time." When they invent a programming language that can do that, we'll all be obsolete as there will just be one button on the screen which says "CLICK HERE TO DO WHAT YOU ARE THINKING ABOUT" and that'll be it :-P

Orion Edwards
Definitely all valid points (hence the upvote). But check out Glenn's answer, it certainly bears merit.
Chris Bunch
+4  A: 

What if you turn your problem around? Instead of trying to get names from variables, get the variables from the names:

["foo", "goo", "bar"].each { |param_name|
  param = eval(param_name)
  if param.class != Array
    puts "#{param_name} wasn't an Array. It was a/an #{param.class}"
    return "Error: #{param_name} wasn't an Array"
  end
  }

If there were a chance of one the variables not being defined at all (as opposed to not being an array), you would want to add "rescue nil" to the end of the "param = ..." line to keep the eval from throwing an exception...

glenn mcdonald
A very interesting way to solve this problem! Thanks Glenn!
Chris Bunch
+1  A: 

Foo is only a location to hold a pointer to the data. The data has no knowledge of what points at it. In Smalltalk systems you could ask the VM for all pointers to an object, but that would only get you the object that contained the foo variable, not foo itself. There is no real way to reference a vaiable in Ruby. As mentioned by one answer you can stil place a tag in the data that references where it came from or such, but generally that is not a good apporach to most problems. You can use a hash to receive the values in the first place, or use a hash to pass to your loop so you know the argument name for validation purposes as in DBR's answer.

Michael Latta
+1  A: 

The closest thing to a real answer to you question is to use the Enumerable method each_with_index instead of each, thusly:

my_array = [foo, baz, bar]
my_array.each_with_index do |item, index|
  if item.class != Array
    puts "#{my_array[index]} wasn't an Array. It was a/an #{item.class}"
  end
end

I removed the return statement from the block you were passing to each/each_with_index because it didn't do/mean anything. Each and each_with_index both return the array on which they were operating.

There's also something about scope in blocks worth noting here: if you've defined a variable outside of the block, it will be available within it. In other words, you could refer to foo, bar, and baz directly inside the block. The converse is not true: variables that you create for the first time inside the block will not be available outside of it.

Finally, the do/end syntax is preferred for multi-line blocks, but that's simply a matter of style, though it is universal in ruby code of any recent vintage.

Greg Borenstein