# Working with kernel.eval

**URL:** <https://rubytalk.org/t/working-with-kernel-eval/24871>\
**Category:** ruby-talk\
**Created:** [13 February 2006 20:07 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871 "2006-02-13T20:07:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Whitney](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@Ian\_Whitney](https://rubytalk.org/u/Ian_Whitney)\
**Post date:** [13 February 2006 20:07 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/1 "2006-02-13T20:07:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kent\_Sibilev1](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Kent\_Sibilev1](https://rubytalk.org/u/Kent_Sibilev1)\
**Post date:** [13 February 2006 20:11 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/2 "2006-02-13T20:11:28Z")

</div>

$ irb  
irb(main):001:0\> 2/3  
=\> 0  
irb(main):002:0\> 2.0/3  
=\> 0.666666666666667  
irb(main):003:0\>

Kent.

> **···**
>
> On 2/13/06, Ian Whitney \<iwhitney@ssa-i.org\> 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.
> > 
> > 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

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [13 February 2006 20:15 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/3 "2006-02-13T20:15:11Z")

</div>

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.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [13 February 2006 20:19 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/4 "2006-02-13T20:19:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ian\_Whitney](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@Ian\_Whitney](https://rubytalk.org/u/Ian_Whitney)\
**Post date:** [13 February 2006 20:24 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/5 "2006-02-13T20:24:22Z")

</div>

This works and I appreciate the response. But it doesn't solve the problem that the formulas in the text files already exist & they weren't written with Ruby's arithmetic in mind. My life would be easier if I didn't have to go update all of those text files.

Any thoughts on how I can take the string as it stands, "1+2/3" and get the right answer? I could use regex to replace every integer with a floating point number, I guess. But that seems kludgey.

> **···**
>
> On Feb 13, 2006, at 2:15 PM, Joel VanderWerf wrote:
> 
> > 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.
> > 
> > --  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Ian\_Whitney](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@Ian\_Whitney](https://rubytalk.org/u/Ian_Whitney)\
**Post date:** [14 February 2006 02:58 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/6 "2006-02-14T02:58:47Z")

</div>

That does appear to be the best solution. I plugged that in & my unit tests look good.

Thanks,

Ian

> **···**
>
> On Feb 13, 2006, at 2:19 PM, David Vallner wrote:
> 
> > 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

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [13 February 2006 20:29 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/7 "2006-02-13T20:29:32Z")

</div>

Well, depending on how many operators you need to support, it's fairly easy to parse them into an Abstract Syntax Tree and run the calculation.

James Edward Gray II

> **···**
>
> On Feb 13, 2006, at 2:24 PM, Ian Whitney wrote:
> 
> > Any thoughts on how I can take the string as it stands, "1+2/3" and get the right answer?

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [13 February 2006 21:09 UTC](https://rubytalk.org/t/working-with-kernel-eval/24871/8 "2006-02-13T21:09:42Z")

</div>

Ian Whitney wrote:

> Any thoughts on how I can take the string as it stands, "1+2/3" and get  
> the right answer? I could use regex to replace every integer with a  
> floating point number, I guess. But that seems kludgey.

David's suggestion to require 'mathn' is a good way to go, but be aware  
that 2/3 will evaluate to a rational--precise, but in general slower to  
compute with.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
