From: Charles Nutter Date: 2011-01-12T03:14:01+09:00 Subject: [ruby-core:34374] [Ruby 1.9-Feature#4264] General type coercion protocol for Ruby Issue #4264 has been updated by Charles Nutter. A question was raised about whether #to should understand context, as in knowing a top-level object being coerced to YAML should include the --- header. The answer seems to come from Marshal. Marshal.dump(object) knows how to produce the marshal header, linking, and so on. Marshal is the master of this format. But Marshal defers to the objects themselves for actual *content* to go into that marshaled output, calling _dump or marshal_dump on each object in turn. With the #to protocol, Marshal.dump could be implemented as: def Marshal.dump(obj) emit_header obj.to(Marshal) end In the same way that marshal_dump is used today. Another concern raised is whether #to should be expected on all objects all the time, rather than just always calling MyClass.coerce(obj). The answer is simple: you want individual source types to control how they coerce to a target type, rather than expecting the target type to know about all possible sources. The default protocol where #to calls type.coerce would simply be a default behavior. ---------------------------------------- http://redmine.ruby-lang.org/issues/show/4264 ---------------------------------------- http://redmine.ruby-lang.org