Get all instance variables declared in class

ruby

Solution

You can use `instance_variables`:

A.instance_variables

but that’s probably not what you want, since that gets the instance variables in the class `A`, not an instance of that class. So you probably want:

a = A.new
a.instance_variables

But note that just calling `attr_accessor` doesn’t define any instance variables (it just defines methods), so there won’t be any in the instance until you set them explicitly.

a = A.new
a.instance_variables #=> []
a.ab = 'foo'
a.instance_variables #=> [:@ab]

Problem

Please help me get all instance variables declared in a class the same way `instance_methods` shows me all methods available in a class. ``` class A attr_accessor :ab, :ac end puts A.instance_methods #gives ab and ac puts A.something #gives me @ab @ac... ```

Original source