From: Charles Oliver Nutter Date: 2011-01-12T04:59:46+09:00 Subject: [ruby-core:34383] Re: [Ruby 1.9-Feature#4264][Open] General type coercion protocol for Ruby On Tue, Jan 11, 2011 at 12:23 PM, Jim Weirich wrote: > > On Jan 11, 2011, at 1:17 PM, James Edward Gray II wrote: > >> I understood it as to_a/to_s are for conversions. ��They basically mean >> change this object into an Array or a Sring, however it makes sense to do >> that for the current object. >> >> In contrast, to_ary/to_str are for objects that essentially are enhanced >> versions of Array or String. ��They mean, it's OK to pretend this object is >> an Array or a String. >> >> Perhaps I'm wrong though. > > > That's my understanding as well. The logic is inconsistently applied in Ruby right now. Sometimes to_str means treat me as a string without any additional calls, sometimes it's called blindly, sometimes it's checked and called...and sometimes to_s is used instead. However to_str and to_ary are used much more heavily during call protocols (usually by the receiver) than to_a and to_s. The latter two are more heavily used for user-driven conversions. Whether what's described above is correct or not (or desirable), I think the current real-world uses of to_ary/to_str fit my description better. The lack of a formal statement on coercion/conversion protocols is probably responsible for this confusion in the first place. - Charlie