Skip to content

Instantly share code, notes, and snippets.

@schneems
Created June 10, 2012 20:42
Show Gist options
  • Save schneems/2907252 to your computer and use it in GitHub Desktop.
Save schneems/2907252 to your computer and use it in GitHub Desktop.
Get variable name from variable
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