When developing & debugging, I sometimes wish I could write a 1-liner that dumped the names, types & values of a bunch of variables. The problem is I don\'t know ho
No, because foo/bar/baz are not instance variables in your code. They are local variables (instance variables start with a @). There is a way to do it with instance variables and the Object#instance_variables method, though:
@foo = 1
@bar = 2
@baz = 3
instance_variables.each do |var|
value = instance_variable_get var
puts "#{var} = (#{value.class}) #{value}"
end
# outputs:
# @foo = (Fixnum) 1
# @bar = (Fixnum) 2
# @baz = (Fixnum) 3
To get the name of a particular instance variable, loop through all of the instance variables until you find one with a value that matches your instance variable.