--mimepart_4d2da636e1c09_1dc2af2eae0507b
Content-Type: text/plain
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline

Issue #4264 has been updated by Jonas Pfenniger.

File typecast.rb added

I have this version from 2004 that is lying around that does approximately the same. It was included in the factets gem but got pulled out at someime. It uses #cast_to and #cast_from as the methods of coercion. Each conversion is defined as a method either on the instance of the object that calls #cast_to (#cast_to_#{target_class}), or on the target class as #cast_from_#{origin_klass}. I'm not totally happy about it, since it has a strong dependence on the class name, which means that inheriting classes cannot inherit the conversion mechanisms.
----------------------------------------
http://redmine.ruby-lang.org/issues/show/4264

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

--mimepart_4d2da636e1c09_1dc2af2eae0507b
Content-Type: text/x-ruby-script; name=typecast.rb
Content-Transfer-Encoding: Base64
Content-Disposition: attachment; filename=typecast.rb

IyMjCiMgICBDb3B5cmlnaHQgMjAxMCBTVFZTIFNBIDxzdHZzQHN0dnMuY2g+
CiMjIwojID0gdHlwZWNhc3QucmIKIwojID09IENvcHlyaWdodCAoYykgMjAw
NCBKb25hcyBQZmVubmlnZXIKIwojICAgUnVieSBMaWNlbnNlCiMKIyAgIFRo
aXMgbW9kdWxlIGlzIGZyZWUgc29mdHdhcmUuIFlvdSBtYXkgdXNlLCBtb2Rp
ZnksIGFuZC9vciByZWRpc3RyaWJ1dGUgdGhpcwojICAgc29mdHdhcmUgdW5k
ZXIgdGhlIHNhbWUgdGVybXMgYXMgUnVieS4KIwojICAgVGhpcyBwcm9ncmFt
IGlzIGRpc3RyaWJ1dGVkIGluIHRoZSBob3BlIHRoYXQgaXQgd2lsbCBiZSB1
c2VmdWwsIGJ1dCBXSVRIT1VUCiMgICBBTlkgV0FSUkFOVFk7IHdpdGhvdXQg
ZXZlbiB0aGUgaW1wbGllZCB3YXJyYW50eSBvZiBNRVJDSEFOVEFCSUxJVFkg
b3IgRklUTkVTUwojICAgRk9SIEEgUEFSVElDVUxBUiBQVVJQT1NFLgojCiMg
PT0gQXV0aG9yKHMp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