Reputation: 6761
I've got an ActiveModel class SupportCase
which has certain instance variables (created with attr_accessor
).
For a given object of that class, how I can print out the names of its instance variables as well as the values.
I've tried:
support_case.instance_variables.each { |a,b| puts "#{a} #{b}" }
support_case.instance_variables.each { |a| puts "#{a} #{support_case.send(a)}" }
The latter didn't work since it creates the call support_case.@var
whereas it should be support_case.var
. So I could strip the @
but
there has to be some cleaner way of doinf what I want to do, right?
Thanks!
Upvotes: 0
Views: 1331
Reputation: 6761
Thanks to Divya Sharma, I've found the method I was looking for:
#instance_variable_get
I used in the following way (regarding my question):
support_case.instance_variables.each { |ivar| puts "#{ivar}: #{support_case.instance_variable_get(ivar)}" }
Upvotes: 1