CALL US ⁄ ⁄   +61 3 9995 0765



Both inspect and to_s methods seem to be returning a string representation of the object they’re being called on. Today, Texo Design - web design company melbourne will share you topic “Inspect and to_s in Ruby classes”

For someone new to Ruby, maybe you will have a little bit unclear on practical difference between inspect and to_s methods. Why so many methods that do eventually them same thing? or they do not? How they differ?

For instance above, sometimes the distinction does not make much sense at all; it’s just two variant string representations.

.inspect method

#.inspect method returns a string containing a human-readable representation of object. By default, show the class name and the list of the instance variables and their values (by calling #inspect on each of them). If you defined classes, you should override this method to make better representation of object. When overriding this method, it should return a string whose encoding is compatible with the default external encoding.

#.inspect is intended for debugging purposes. It’s always a sort of developers’s debugging output of something to see what it is.

.to_s method
#.to_s method returns a string representing of object. The default to_s prints the object’s class and an encoding of the object id. As a special case, the top-level object that is the initial execution context of Ruby programs returns “main”.

Customize .inspect and .to_s methods
 I’m going to create an example program asks for your name and greeting you:

 According to the official Ruby documentation, gets returns a string object read from standard input and then .strip removes any leading and trailing whitespace (e.g spaces, new lines..)

The built-in print and puts methods print a string representation of any object that you pass to it.

Now we’re going to rewrite example above by your own class, to encapsulate the concept of Person

This example just returns the string object stored in the @name instance variable but with a custom #to_s method, you can define any behavior that you want:

 Customize .inspect for debugging purposes

You’ve learned how to customize output on display on screen, but there’s case where your custom #to_s still fall short when you have an error that involves your object.

Try to run following code, you will get an error with NoMethodError

Ruby uses #inspect in situations where you might want to get a bit more info on the objects than the simple string representation you use for regular output.

Define a custom #inspect method; It will show you that extra information whenever you have an error involving that object:

Output is very important in any application, it’s also very helpful helping you debugging output of something to see what it is, what is happening with our application. And knowing the different ways you can customize an object’s output will help you write programs that work perfectly.

Source preferences

Texo Design is one of the renowned names in web design digital agency. We are the team of well – qualified and experienced staff as we deal with mobile apps, web development and social media campaign. We offer you the best tailor-made solution. Your success is our guarantee as we commit quality, professionalism and communication to our clients. These three pillars will help you in making your formal decision. We transform your ideas into reality with highly innovative technologies and experts mindset. Contact us +61 3 9995 0765 or for more information

Be the first to post on the blog

Leave a Reply


Back to blog