In Ruby, how to write a method to display any object's instance variable names and its values

Posted by Jian Lin on Stack Overflow See other posts from Stack Overflow or by Jian Lin
Published on 2010-06-16T23:02:37Z Indexed on 2010/06/16 23:12 UTC
Read the original article Hit count: 144

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")

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby