Ruby: inspect method

When you try and look at an object directly you’ll get something cryptic like #<someObject:0x12345678>. 

To make it more meaningful you can override the inspect method, e.g. 

class Person

attr_reader :name

def initialize(name)

@name = name

end

def inspect 

“This person’s name is #{@name}”

end

end

a = Person.new(“Rafa”)

Then:

a

will give:

This person’s name is Rafa

Leave a Reply

Your email address will not be published. Required fields are marked *