From: Robert Klemme Date: 2008-02-09T07:05:01+09:00 Subject: Re: converting strings to booleans, symbols, etc On 08.02.2008 19:51, an an wrote: > Hi, > > I am parsing an xml document, and I wanted to know if there is a quick > built in way to convert a string to a symbol or a boolean. an example > xml doc would be: > > data > > Now I know I can do some string compares, and set the variable > accordingly, but I was wondering if there are shortcuts, much like the > to_i method. You can create yourself some. One example: CONV = { "false" => false, "true" => true, "0" => 0, "1" => 1, # more common / important values } def convert(s) x = CONV[s] if x.nil? x = case x when /\A[+-]?\d+\z/ s.to_i when /\A[+-]?\d+(?:\.\d+)?\z/ s.to_f # more else x end end x end Kind regards robert