Issue #4264 has been updated by mame (Yusuke Endoh).


And I'd like to add some question.

== Why do you want to remove the monkey-patching?

If you are worried about method name conflict, this proposal does not
solve the problem because the class names still collide.

If you dislike monkey-patching just because it is monkey-patching,
refinement (#4085) may be more general solution.


== Method name

I understand you want to avoid bikeshed, but I think it is inevitable
in this proposal.


== Performance

I don't think this is so big problem :-)

-- 
Yusuke Endoh <mame / tsg.ne.jp>
----------------------------------------
Feature #4264: General type coercion protocol for Ruby
https://bugs.ruby-lang.org/issues/4264#change-25661

Author: headius (Charles Nutter)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 


=begin
 Justification: Ruby objects variously define to_ary, to_int and others for either explicit (before call) or implicit (during call) coercion. However, the set of methods is limited, and adding more names just clutters namespaces and isn't feasible long-term anyway. This proposal will hopefully start a discussion toward adding a general type-coercion protocol via a #to or #to_any method. To blunt the naming discussion a bit, I will refer to it as #to_x.
 
 Description: The #to_x method will be a "supermethod" of sorts that can be used to coerce a given object to an arbitrary type. Where currently there are specific methods for coercing to specific types (to_ary, to_str), and other more general methods intended not for coercion but for explicitly re-structuring an object's data (to_a, to_s), there's no one protocol for doing general coercion. #to_x would fill the roles of the coercion methods, accepting a target class and responding appropriately.
 
 The response will depend on whether the target object can be coerced to the given type. The result for success should obviously be an instance of the target type. The result for failure could either be "soft": returning nil, or "hard": raising an error. There could also be an optional boolean flag that specifies hard or soft.
 
 Existing coercion methods could (but need not be) implemented in terms of #to_x
 
 def to_ary
   to_x(Array)
 end
 
 def to_str
   to_x(String)
 end
 
 Prior art: JRuby supports coercing Ruby objects to arbitrary Java types in this way. Currently only a set of hard-coded target types are supported for various core Ruby classes, but this is intended to eventually be part of the invocation protocol when calling Java. In other words, if the object being passed is not the exact type of the target parameter, JRuby will invoke to_java(target_param_type) to do the coercion. Performance implications in this are obvious...so there may need to be discussions about modifying this protocol to make it easier to optimize.
=end



-- 
http://bugs.ruby-lang.org/