In Ruby, how to write a method to display any object's instance variable names and its values
- by Jian Lin
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>
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")