How to get the Name of a Variable

Hello.

When I call a function with an argument, I want to retrieve the NAME of
the original argument I passed into the funtion. In the code snippet
below, I want the last line of output to be:

The NAME of the variable you passed in is: local_var

instead of

The NAME of the variable you passed in is: WHAT DO I NEED HERE?

Thank you!

<code>

def a_not_so_special_function(a)
puts "You are inside a_not_so_special_function().\n"
puts "The VALUE of the variable you passed in is: "+a
puts "The NAME of the variable you passed in is: "+"WHAT DO I NEED
HERE?"
end

local_var="foo"
a_not_so_special_function(local_var)

</code>

__OUTPUT__

You are inside a_not_so_special_function().
The VALUE of the variable you passed in is: foo
The NAME of the variable you passed in is: WHAT DO I NEED HERE?

···

--
Posted via http://www.ruby-forum.com/.

I suppose I'd have to ask why this is necessary. If you need a name associated with a value I'd recommend using a hash instead. Technically there might be a way be analyzing stack frames, but that would require access to the internals through the C API.

Regards,
Chris White
http://www.twitter.com/cwgem

···

On Aug 13, 2011, at 2:17 AM, Agent Mulder wrote:

Hello.

When I call a function with an argument, I want to retrieve the NAME of
the original argument I passed into the funtion. In the code snippet
below, I want the last line of output to be:

The NAME of the variable you passed in is: local_var

instead of

The NAME of the variable you passed in is: WHAT DO I NEED HERE?

Thank you!

<code>

def a_not_so_special_function(a)
puts "You are inside a_not_so_special_function().\n"
puts "The VALUE of the variable you passed in is: "+a
puts "The NAME of the variable you passed in is: "+"WHAT DO I NEED
HERE?"
end

local_var="foo"
a_not_so_special_function(local_var)

</code>

I generate a complex data structure from more simple
primitives.

That's beginning programming in any language.

Having the names around makes it easier to get the
desired results.

Wrong. All the time. In any language.

···

--
Posted via http://www.ruby-forum.com/\.

I suppose I'd have to ask why this is necessary. If you need a name
associated with a value I'd recommend using a hash instead. Technically
there might be a way be analyzing stack frames, but that would require
access to the internals through the C API.

I need it because I generate a complex data structure from more simple
primitives. Having the names around makes it easier to get the desired
results. Using hashes will work, but it adds noise to the algorithms,
making them harder to construct. But if a hash is the simplest way of
getting what I want, then a hash it will be...

Thank you for replying!

···

--
Posted via http://www.ruby-forum.com/\.

I suppose I'd have to ask why this is necessary. If you need a name
associated with a value I'd recommend using a hash instead. Technically
there might be a way be analyzing stack frames, but that would require
access to the internals through the C API.

I need it because I generate a complex data structure from more simple
primitives. Having the names around makes it easier to get the desired
results.

What exactly are "the desired results"? Can you elaborate a bit more
on what you do? Also a http://sscce.org/ might help.

Using hashes will work, but it adds noise to the algorithms,
making them harder to construct. But if a hash is the simplest way of
getting what I want, then a hash it will be...

Usually if you have dynamic in a way that you generate names of
variables from input then a Hash or similar data structure which
actually stores names and values is the best.

Kind regards

robert

···

On Sat, Aug 13, 2011 at 12:53 PM, Agent Mulder <mbmulder@online.nl> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

*Please*, for the sake of human sanity, *do not* use the following
code in *anything* serious *EVER*. This is only for demonstration
purposes.

def a_not_so_special_function(a)
  fname, line, _ = *caller[0].split(':')
  var_name = File.readlines('./'+fname)[line.to_i-1].match(/a_not_so_special_function[\(\s]([a-zA-Z0-9_]+)/)[1]
  
  puts "You are inside a_not_so_special_function().\n"
  puts "The VALUE of the variable you passed in is: "+a
  puts "The NAME of the variable you passed in is: "+var_name
end

local_var="foo"
a_not_so_special_function(local_var)

In short - first line of the function uses a Kernel method 'caller' to
figure out in which line of which file the function itself was called.
Second lines uses this information to read the file, find the correct
line and guess-parse it to find the variable name.

This is going to fail if the function is called in any funny manner.

And once again, please, do not use this code. If you don't want
hashes, maybe it makes more sense to pass the actual name of variable
to the function, and do some eval() magic to find out the value? This
is definitely less brain-damaging solution than the above.

-- Matma Rex

I'll cheat by not using a closure... but here's what I came up with.

class Test

  yummy = "cereal"

  @function = lambda do |arg|
    puts "The value of arg is " + arg
    local_variables.each do |var|
      if eval(var.to_s) == arg && var != :arg
        puts "The name of arg is " + var.to_s
      end
    end
    nil
  end

  p @function.call(yummy)

end

If there are lots of local variables in scope then it'd be a good idea
to memoize them. Using a hash still makes the most sense though.

-Luke

···

--
Posted via http://www.ruby-forum.com/.