From: "mame (Yusuke Endoh)" Date: 2012-04-05T07:45:21+09:00 Subject: [ruby-core:44134] [ruby-trunk - Feature #4264] General type coercion protocol for Ruby Issue #4264 has been updated by mame (Yusuke Endoh). =begin Hello, headius Thank you. Is your problem about just monkey-patching? Please check the following summary. == Problem Many monkey-patching like below is used in some libraries, such as xml and json: class Integer def to_json ... end end class Array def to_json ... end end ... The motivation of this proposal is to remove these monkey-patching. == Proposal You want Ruby to provide the following simple one method: class Object def convert_to(kls) kls.convert(self) end end The method name is still arguable, though. == How the problem is solved The libraries only have to define the following one method, instead of a lot of monkey-patching. class JSON def convert(obj) case obj when Integer ... when Array ... end end end Note that a user should write convert_to(JSON) instead of to_json. =end ---------------------------------------- Feature #4264: General type coercion protocol for Ruby https://bugs.ruby-lang.org/issues/4264#change-25660 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/