Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is overriding to_s methods in Ruby bad? [closed]

Tags:

ruby

I've been experimenting and find that I like redefining Object's to_s methods.

Is this a bad idea or is it good practice?

like image 203
fivetwentysix Avatar asked May 16 '11 08:05

fivetwentysix


People also ask

What is TO_S method in Ruby?

The to_s function in Ruby returns a string containing the place-value representation of int with radix base (between 2 and 36). If no base is provided in the parameter then it assumes the base to be 10 and returns.

How do you override a class in Ruby?

Override means two methods having same name but doing different tasks. It means that one of the methods overrides another method. If there is any method in the superclass and a method with the same name in its subclass, then by executing these methods, method of the corresponding class will be executed.


1 Answers

No, you should feel free to override to_s - there are no ill side-effects. As long as your new to_s is more informative than the built-in (not exactly a high standard there), you're in the clear.

And they help make your test failures read better - sometimes by a lot - which is never a bad thing. Go for it!

like image 195
Magnar Avatar answered Sep 21 '22 00:09

Magnar