From: David Vallner Date: 2006-02-14T05:19:00+09:00 Subject: Re: Working with kernel.eval require "mathn" for a batch of tweaks that will make Ruby maths expression more maths-like than C-like. David Vallner Dňa Pondelok 13 Február 2006 21:07 Ian Whitney napísal: > 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. > > I thought maybe that eval wasn't following the standard order of > operations, which could explain the return value of 1. So then I > tried this: > > irb(main):005:0> a = "2/3" > => "2/3" > irb(main):006:0> b = eval(a) > => 0 > irb(main):007:0> b.class > => Fixnum > > > So, it's not an order of operations thing. > > Maybe I shouldn't be using eval. But I can't find another option. Any > thoughts? > > Thanks, > > Ian