From: Paul Brannan Date: 2005-09-08T01:37:08+09:00 Subject: Re: Lexical Casts with Ruby On Sun, Sep 04, 2005 at 12:16:26AM +0900, R??diger Sonderfeld wrote: > Hello, > I want to assign some values I recive in string form to attributes of a > user specified class. The problem is that I have to lexical cast the > strings into the required type. I can't use to_i or to_f because the attribute > type can only be determined at runtime. > > Is there a way for lexical casts between types in Ruby? Something like > convert("10", Fixnum) > => 10 In C++, boost::lexical_cast uses streams to convert the object to a string and then from the string to the desired type. It is easy to do the first part in ruby; all objects have a to_s method. The second part is harder. There is no uniform mechanism for converting a string into an object. Paul