Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
How to customize inspect method?
4 Posts
How to customize inspect method?

Hi,

puts object.yaml renders object's properties slight nice than inspect method.Actually I need object properties in a structured fashion like print_r() of PHP function does.

Thanks for reply

Person
Forum Rating: 100
Sign In To Rate Post

Simply redefine the inspect method.

class YourObjectClass def inspect

# build the string you want inspect to return

end end

As simple as that. Also you can use .to_yml as an alternative.

Cheers

I think pp object or puts object.to_yml looks much better.

Dear All, I need to work with inspect method to customize it because inspect method does not print-out object's properties nicely(PHP print_r() method prints object's properties nicely and readably). So I need to customize inspect method. I know so far I have to work with kernel,Please please help me with this issue

Person
Forum Rating: 100
Sign In To Rate Post
4 Posts
Login to add your message