From: Eric Mahurin Date: 2005-09-09T03:49:59+09:00 Subject: Re: Lexical Casts with Ruby --- Paul Brannan wrote: > On Thu, Sep 08, 2005 at 02:09:32AM +0900, Eric Mahurin wrote: > > so you could do: > > > > "10".to(Integer) # 10 > > String.from(10) # "10" > > > > If this is a good solution for you, maybe an RCR for this > more > > general conversion mechanism is in order. > > See RCR#280. The implementation is 34 lines long. Don't know why I missed that one. I don't really like the implementation with the global (or rather class variable) hash. I would rather see an encapsulated API - each class gives methods for converting to/from objects of other classes of interest (i.e. String). You may have duplicated code or one from_* calling another to_* (or vice-versa), but so what. This way requires 0 lines of overhead - nothing needs to manage framework. It's simple and is an extension of what we already have. The klass#to(toKlass) and klass.from(fromObj) are shortcuts to get a little more abstract if you want: class String def self.from(obj); obj.to_s; end def to(klass); klass.from_s(self); end end If you did this, then I guess you could consider this the overhead for each class that other classes want to convert to/from - String, Integer, Float, Array, etc. I would think this more straight-forward approach would be more likely accepted. ______________________________________________________ Click here to donate to the Hurricane Katrina relief effort. http://store.yahoo.com/redcross-donate3/