Given any object in Ruby (on Rails), how can I write a method so that it will display that object’s instance variable names and its values, like this:
@x: 1
@y: 2
@link_to_point: #<Point:0x10031b298 @y=20, @x=38>
(Update: inspect will do except for large object it is difficult to break down the variables from the 200 lines of output, like in Rails, when you request.inspect or self.inspect in the ActionView object)
I also want to be able to print <br> to the end of each instance variable’s value so as to print them out nicely on a webpage.
the difficulty now seems to be that not every instance variable has an accessor, so it can’t be called with obj.send(var_name)
(the var_name has the “@” removed, so “@x” becomes “x”)
Update: I suppose using recursion, it can print out a more advanced version:
#<Point:0x10031b462>
@x: 1
@y: 2
@link_to_point: #<Point:0x10031b298>
@x=38
@y=20
I see… Antal must be giving the advanced version here…
the short version then probably is:
or to return it as a string:
or shorter: