Is it possible to print variable's name and how?
asdf = Hash.new
asdf = Hash.new
print_name asdf # => asdf
print_name fdsa # => fdsa
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:
asdf
and return nil, because Kernel#puts always returns nil.
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"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments