How to print variable's name?

Sato

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

asdf = Hash.new
asdf = Hash.new

print_name asdf  # => asdf
print_name fdsa  # => fdsa
Todd A. Jacobs

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:

asdf

and return nil, because Kernel#puts always returns nil.

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"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Print a variable's name

From Dev

Print a variable's name

From Dev

Python: Print a variable's name and value?

From Dev

How to print the variable name along with its value?

From Dev

Print / display a JavaScript variable's name instead of it's value

From Dev

How do i print Variable name instead of the value assigned to the variable?

From Dev

How to create a variable inside a variable's name?

From Dev

Bash - How to use a variable in a variable's name

From Dev

How to print a word (URL name) that appears multiple times in a TCL variable?

From Dev

How to print specific output based on condition from name variable

From Dev

How to convert text into variable name to print its value using innerHTML?

From Dev

How to log a variable's name and value?

From Dev

How to create an object with a variable as it's name?

From Dev

How to escape variable name in PHP's Heredoc?

From Dev

function to print name of passed variable

From Dev

Using print as a variable name in python

From Dev

How do Chrome and Firefox print the object's class name in the console?

From Dev

How can I use a variable's value in a variable name in JavaScript?

From Dev

How do i use variable's value as a dynamic variable name?

From Dev

how to print the name of the files

From Dev

How to initialize a Class-instance and print it's public variable?

From Dev

How to print a variable with perror

From Dev

How do you print a variable in PHP that's either one element of an array, or the whole variable if it's not an array

From Dev

Python: a way to avoid <<print "variable_name =", variable_name>>?

From Dev

How can we print the variable name along with its value in python, which will be useful during debugging?

From Dev

How do I identify the variable's name as a string from self?

From Dev

How to get the variable's name from the llvm instruction

From Dev

How to use a method's string argument as a method name variable

From Dev

How to find the current batch file's name and store as a variable?

Related Related

  1. 1

    Print a variable's name

  2. 2

    Print a variable's name

  3. 3

    Python: Print a variable's name and value?

  4. 4

    How to print the variable name along with its value?

  5. 5

    Print / display a JavaScript variable's name instead of it's value

  6. 6

    How do i print Variable name instead of the value assigned to the variable?

  7. 7

    How to create a variable inside a variable's name?

  8. 8

    Bash - How to use a variable in a variable's name

  9. 9

    How to print a word (URL name) that appears multiple times in a TCL variable?

  10. 10

    How to print specific output based on condition from name variable

  11. 11

    How to convert text into variable name to print its value using innerHTML?

  12. 12

    How to log a variable's name and value?

  13. 13

    How to create an object with a variable as it's name?

  14. 14

    How to escape variable name in PHP's Heredoc?

  15. 15

    function to print name of passed variable

  16. 16

    Using print as a variable name in python

  17. 17

    How do Chrome and Firefox print the object's class name in the console?

  18. 18

    How can I use a variable's value in a variable name in JavaScript?

  19. 19

    How do i use variable's value as a dynamic variable name?

  20. 20

    how to print the name of the files

  21. 21

    How to initialize a Class-instance and print it's public variable?

  22. 22

    How to print a variable with perror

  23. 23

    How do you print a variable in PHP that's either one element of an array, or the whole variable if it's not an array

  24. 24

    Python: a way to avoid <<print "variable_name =", variable_name>>?

  25. 25

    How can we print the variable name along with its value in python, which will be useful during debugging?

  26. 26

    How do I identify the variable's name as a string from self?

  27. 27

    How to get the variable's name from the llvm instruction

  28. 28

    How to use a method's string argument as a method name variable

  29. 29

    How to find the current batch file's name and store as a variable?

HotTag

Archive