From: Josh Cheek Date: 2012-09-07T15:51:38+09:00 Subject: Re: Doing math in Ruby --bcaec517a9d44f620504c9170906 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Sep 6, 2012 at 8:52 PM, Jon M. wrote: > Hello everybody. I am new to Ruby and understand the bare minimum of > basic concepts. I was wondering if there was a way to take the input of > a user (supposedly a mathematical equation) and return the answer. For > example, if I started the Ruby script in my Terminal, is there a way > that I would be able to type in 1+2 and get a returned answer of 3? > Something I tried was: > > puts "Enter Equation" > answervar = gets.chomp > doneans = answervar.to_i > puts doneans > > Yet, all that did was return the first number of the equation. Any help > or advice would be greatly appreciated. Thanks! > > -- > Posted via http://www.ruby-forum.com/. > > If it's just a tool for you to use, then I'd just eval the inputs (does constrain the syntax a bit, though) If you want to take in user input, then you'll need to do some kind of parsing of the input. A quick search found http://lukaszwrobel.pl/blog/math-parser-part-4-tests which looks pretty simple, but might work. Otherwise, try searching through rubygems.org and ruby-toolbox.com --bcaec517a9d44f620504c9170906 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Thu, Sep 6, 2012 at 8:52 PM, Jon M. <lists@ruby-forum.com> wrote:
Hello everybody. I am new to Ruby and understand the bare minimum of
basic concepts. I was wondering if there was a way to take the input of
a user (supposedly a mathematical equation) and return the answer. For
example, if I started the Ruby script in my Terminal, is there a way
that I would be able to type in 1+2 and get a returned answer of 3?
Something I tried was:

puts "Enter Equation"
answervar =3D gets.chomp
doneans =3D answervar.to_i
puts doneans

Yet, all that did was return the first number of the equation. Any help
or advice would be greatly appreciated. Thanks!

--
Posted via http://= www.ruby-forum.com/.



If it's just a tool for you to= use, then I'd just eval the inputs (does constrain the syntax a bit, t= hough)

If you want to take in user input, then you'll need to do= some kind of parsing of the input. A quick search found http://lukaszwrobel.pl/blog/= math-parser-part-4-tests which looks pretty simple, but might work. Oth= erwise, try searching through rubygems.org<= /a> and ruby-toolbox.com
--bcaec517a9d44f620504c9170906--