From: Yukihiro Matsumoto Date: 2011-01-13T01:11:51+09:00 Subject: [ruby-core:34437] Re: [Ruby 1.9-Feature#4264][Open] General type coercion protocol for Ruby Hi, In message "Re: [ruby-core:34404] Re: [Ruby 1.9-Feature#4264][Open] General type coercion protocol for Ruby" on Wed, 12 Jan 2011 14:09:47 +0900, Charles Oliver Nutter writes: |I guess I'm confused what the difference is. For example, sometimes |to_ary is used as a marker, and not invoked. Sometimes it is invoked |and expected to return an array. There are many cases in RubySpec |showing both behaviors. So it seems to be a more narrow conversion |method, but it still seems like a conversion method to me. Now the world of Ruby is huge, so I could not be responsible for all, but the original intention of to_ary and alike are implicit conversion to retrieve C structure for C defined methods. Using it as a mere marker is not a good idea, I think. |Can you provide more clarification: | |* Are to_ary/to_str/to_int intended only for use from core methods? Basically they are for C implemented methods. Ruby implemented method should based on duck typing. |* Are they expected to always return the an instance of the appropriate type? They are expected to return an instance of appropriate type all the time, no exception. If you cannot provide corresponding object, you should not define to_ary for the class. |* Should users be defining these methods to produce new structures in |the same way as they define to_a, or should they only be doing so if |their class can duck-type as an array/string/integer for all |array/string/integer methods? I strongly suggest that users should define to_ary only when the object behaves just like an array. Did I make myself clear? matz.