small medium large xlarge

27 Sep 2008, 08:50
Ricardo Perez Lopez (3 posts)

I don’t understand what’s the difference between to_s and to_str (the same for to_i - to_int, to_a - to_ary, and so), and why we need both method styles. I found it very confusing, against the KISS principle even. Can anybody explain it to me, please?

Thanks in advance.

11 Oct 2008, 02:51
Dave Thomas (366 posts)

The short forms (to_s, to_i, etc) say “do your best to give me a string/integer/… representation of the receiver). The to_str longer-form methods say instead that the receiver can effectively be viewed as a string, and ask for the string representation. So the short forms are permissive, and the long forms will give an error if no exact conversion exists.