From: Robert Klemme Date: 2010-10-19T20:53:24+09:00 Subject: Re: data type for a string value On Tue, Oct 19, 2010 at 12:06 AM, Jack Su wrote: >> The usual solution to this is to expect a number of types (possibly only >> one), parse them and deal with them as needed.  Even if there would be a >> magical mechanism which would determine arbitrary types you would still >> need code to process each type properly. > > it would be nice to have a method String#parse. > > '12/31/1999'.parse() => a date object > '$1.23'.parse() => a float object > '32.1%'.parse() => a float object No, that would be a catastrophe! Class String is responsible for string handling in general. Everybody has different requirements for parsing strings (if at all) so there would be no way to implement this in the standard library in any reasonable way. Just think about types which have different representations (e.g. Time and DateTime, Float and BigDecimal) let alone the question how many types are detected. As Xavier said already: the problem is too generic. >> What kind of problem are you really trying to solve? > > for parsing data from spreadsheets (or csvs for that matter) to sort > data based on their types, e.g. 21 > 3 not '21' < '3'. If you directly pull it from a spreadsheet there seems to be a minimal chance that you can obtain type information from it. If you pull it from CSV you need to define which types you want to recognize and code accordingly. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/