TwP / inifile

Native Ruby package for reading and writing INI files
http://codeforpeople.rubyforge.org/inifile
95 stars 47 forks source link

Typecasting values #22

Closed dennismonsewicz closed 10 years ago

dennismonsewicz commented 10 years ago

This isn't a bug but more of a feature request.

Would love to see a typecasting method to allow for values like 4.5, 6, true, false, etc

Here is a method that I use for simple typecasting:

def typecast(value)
  case value
    when /^\s*$/                                        then nil
    when /^-?(?:\d|[1-9]\d+)$/                          then Integer(value)
    when /^-?(?:\d|[1-9]\d+)(?:\.\d+)?(?:e[+-]?\d+)?$/i then Float(value)
    when /true/i                                        then true
    when /false/i                                       then false
    else                                                     value
  end
end