Created
June 10, 2012 20:42
-
-
Save schneems/2907252 to your computer and use it in GitHub Desktop.
Get variable name from variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo = 'i ama string' | |
var_type = defined? foo | |
variable_list = case var_type | |
when "local-variable" | |
local_variables | |
when "instance-variable" | |
instance_variables | |
when "global-variable" | |
gloval_variables | |
else | |
[] | |
end | |
var_name = variable_list.detect {|v| eval(v.to_s) == foo} | |
puts "The value of the '#{var_name}' variable is '#{foo}'" | |
#=> The value of the 'foo' variable is 'i ama string' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment