From: Joel VanderWerf Date: 2006-02-14T05:15:11+09:00 Subject: Re: Working with kernel.eval Ian Whitney wrote: > I'm trying to calculate the results of formulas that are stored in text > files. > > For example, a file may contain the line: 1+2/3 > > I want to take that line & get the result of the formula, 1.66 (roughly) > > The way to do this seems to be Kernel.eval. But I can't get it to work > correctly. Here's my irb session: > > irb(main):001:0> a = "1+2/3" > => "1+2/3" > irb(main):002:0> b = eval(a) > => 1 > irb(main):004:0> b.class > => Fixnum > > I would think that eval(a) should return a Float, not a Fixnum. It's nothing to do with eval, but rather that if you start with fixnums, you do fixnum arithmetic. Try replacing 2 with 2.0. That invokes float division, and every result depending on that input will be float. In general, you can use x.to_f if you want to force a value to be treated as float. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407