Is it possible to print variable's name and how?
asdf = Hash.new
asdf = Hash.new
print_name asdf # => asdf
print_name fdsa # => fdsa
Is it possible to print variable's name and how?
asdf = Hash.new
asdf = Hash.new
print_name asdf # => asdf
print_name fdsa # => fdsa
This answer originally recommended (and still recommends) a Symbol-based solution to the OP's question. However, it was recently expanded to include quoting as a conceptually simpler approach, as well as some minimal explanations about the internals of how Ruby variables are stored and referenced.
If you already know the name of the variable, you can simply print the variable's Symbol. Ruby will convert this to a String automagically for you when invoking #puts. For example:
asdf = Hash.new
puts :asdf
will print the following on standard output:
asdf
and returns nil
, because Kernel#puts always returns nil
. Use other methods like Kernel#p, Kernel#format, or some other method if you need to return the value in addition to sending it to an I/O stream.
If you prefer to return the name of the variable instead of just printing it, then you need to convert the Symbol to a String. For example:
asdf = Hash.new
:asdf.to_s
#=> "asdf"
In your example, you could also have simply quoted the variables. For example, the following will all do more or less the same thing in slightly different ways because you're printing a String rather than a variable's value:
puts 'asdf'
puts "asdf"
puts %(asdf)
printf "%s\n" % "asdf"
However, I didn't originally suggest quoting because String interpolation, return values from methods, or other common use cases make it less ambiguous to use a Ruby Symbol for printing the variable's name as that's how Ruby stores it internally anyway.
For your given example, it wouldn't make any difference whether you used a Symbol or a quoted String, so quoting was probably the easiest and most straightforward answer that could have been given. On the other hand, using the variable's Symbol has fewer edge cases and allows for more advanced introspection and debugging of your code once you get beyond the basics.
As I pointed out in a comment 7+ years after the original answer was given, all Ruby variables are stored in a Binding, Object, Module, or other scope as a Symbol anyway. Ruby then uses the Symbol to look up the available variables within the current scope, and then uses the Symbol to reference its value. So, not only can you inspect what local, instance, class, and global variables are defined in your current scope, you can also find out what kind of variables they are (e.g. "local-variable", "instance-variable", or "constant") with the defined?
keyword.
The original answer was sufficient for the OP to make it the accepted answer. However, understanding why it works, how variables and symbols are related, and why Symbol-based solutions are recommended for use cases like this one may help other visitors in the future.
I believe you don't need such method, just put the name of the variable by hand.
asdf = Hash.new
puts "Variable `asdf'"
Whenever you want to print the name of a variable, just write it's name. Think about it. You already know the name of the variable and you will have to write it's name either way to reference the variable, it does make sense to have such method, you don't want to duplicate yourself, right?
look
wonderful_method_to_print_variable_name( asdf ) #=> asdf
why not just "asdf"
?
Note, don't be confused with the variable's value, that's another thing.