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!
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
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?
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 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…
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
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.
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.
I’ll cheat by 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.