Following code was tested with ruby 1.9.3 .
All objects have to_s method
to_s method is define in Object class and hence all ruby objects have method to_s.
Certain methods always call to_s method. For example when we do string interpolation then to_s method is called. puts invokes to_s method too.
1class Lab 2 def to_s 3 'to_s' 4 end 5 def to_str 6 'to_str' 7 end 8end 9 10l = Lab.new 11puts "#{l}" #=> to_s 12puts l #=> to_s
to_s is simply the string representation of the object.
Before we look at to_str let's see a case where ruby raises error.
1e = Exception.new('not sufficient fund') 2 3# case 1 4puts e 5 6# case 2 7puts "notice: #{e}" 8 9# case 3 10puts "Notice: " + e
Here is the result
1not sufficient fund 2Notice: not sufficient fund 3`+': can't convert Exception into String (TypeError)
In the first two cases the to_s method of object e was printed.
However in case '3' ruby raised an error.
Let's read the error message again.
1`+': can't convert Exception into String (TypeError)
In this case on the left hand side we have a string object. To this string object we are trying to add object e. Ruby could have called to_s method on e and could have produced the result. But ruby refused to do so.
Ruby refused to do so because it found that the object we are trying to add to string is not of type String. When we call to_s we get the string representation of the string. But the object might or might not be behaving like a string.
Here we are not looking for the string representation of e. What we want is for e to behave a like string. And that is where to_str comes in picture. I have a few more examples to clear this thing so hang in there.
What is to_str
If an object implements to_str method then it is telling the world that my class might not be String but for all practical purposes treat me like a string.
So if we want to make exception object behave like a string then we can add to_str method to it like this.
1e = Exception.new('not sufficient fund') 2 3def e.to_str 4 to_s 5end 6 7puts "Notice: " + e #=> Notice: not sufficient fund
Now when we run the code we do not get any exception.
What would happen if Fixnum has to_str method
Here is an example where ruby raises exception.
1i = 10 2puts '7' + i #=> can't convert Fixnum into String (TypeError)
Here Ruby is saying that Fixnum is not like a string and it should not be added to String.
We can make Fixnum to behave like a string by adding a to_str method.
1class Fixnum 2 def to_str 3 to_s 4 end 5end 6i = 10 7puts '7' + i #=> 710
The practical usage of this example can be seen here.
1irb(main):002:0> ["hello", "world"].join(1) 2TypeError: no implicit conversion of Fixnum into String
In the above case ruby is refusing to invoke to_s on "1" because it knows that adding "1" to a string does not feel right.
However we can add method to_str to Fixnum as shown in the last section and then we will not get any error. In this case the result will be as shown below.
1irb(main):008:0> ["hello", "world"].join(1) 2=> "hello1world"
A real practical example of defining to_str
I tweeted about a quick lesson in to_s vs to_str and a few people asked me to expand on that. Lets see what is happening here.
Before the refactoring was done Path is a subclass of String. So it is String and it has all the methods of a string.
As part of refactoring Path is no longer extending from String. However for all practical purposes it acts like a string. This line is important and I am going to repeat it. For all practical purposes Path here is like a String.
Here we are not talking about the string representation of Path. Here Path is so close to String that practically it can be replaced for a string.
So in order to be like a String class Path should have to_str method and that's exactly what was done as part of refactoring.
During discussion with my friends someone suggested instead of defining to_str tenderlove could have just defined to_s and the result would have been same.
Yes the result would be same whether you have defined to_s or to_str if you doing puts.
1puts Path.new('world')
However in the following case just defining to_s will cause error. Only by having to_str following case will work.
1puts 'hello ' + Path.new('world')
So the difference between defining to_s and to_str is not just what you see in the output.
Conclusion
If a class defines to_str then that class is telling the world that although my class is not String you can treat me like a String.