1

Is it possible to print variable's name and how?

asdf = Hash.new
asdf = Hash.new

print_name asdf  # => asdf
print_name fdsa  # => fdsa
Sato
  • 8,192
  • 17
  • 60
  • 115
  • see this http://stackoverflow.com/questions/2603617/ruby-print-the-variable-name-and-then-its-value – HaveNoDisplayName Jun 05 '15 at 01:31
  • It is not possible. You can *list* variables that are known, but you can't print a variable's name by using the variable, as it would get replaced by its value before the function `print_name` is called. – Amadan Jun 05 '15 at 01:32
  • @HaveNoDisplayName: Note that the linked solution will find all variables with the same value. It can't distinguish which variable you passed. `username1 = "tyndall"; username = username1; username2 = username1;` prints all three variables. – Amadan Jun 05 '15 at 01:41

2 Answers2

0

Brief Summary

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.

Original Recommendations

Print a Symbol

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.

Return a String

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"

Some Additional Context, Seven Years Later

Quoting Variable Names to Print Strings

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.

Why the Symbol Approach Works

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.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • @GrantBirchmeier Your comment is needlessly rude, but we'll treat it as a teachable moment about how Ruby manages variable names under the hood. All Ruby variables are stored in a Binding as a Symbol, which is used to look up the contents of said variable in the appropriate scope. Feel free to read the documentation or the Ruby source code if you don't like the answer, but it's still how Ruby works. Fire up irb and start with `foo = 1; binding.local_variable_get :foo` or `bar = 2; binding.local_variables.grep :bar` and go from there. – Todd A. Jacobs Nov 11 '22 at 02:23
  • Still feels like your answer goes all around the question but doesn't actually answer it. _Is it possible to write the asker's hypothetical `print_name` function?_ It's becoming clear to me that the answer is "no". – Grant Birchmeier Nov 11 '22 at 14:23
-3

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.

yeyo
  • 2,954
  • 2
  • 29
  • 40