This is a card in Dave's Virtual Box of Cards.

Using Ruby as a Calculator

Created: 2024-04-10

The Ruby programming language makes a fantastic command line calculator. I find it much more intuitive to use than tools like bc or dc.

Computers do integer math unless you tell them to do floating point math and many tools and languages reflect this:

bc - the "basic calculator":

$ bc
5 / 3.0
1

Note the integer math, despite dividing by a value with a decimal point.

dc - the RPN "desk calculator":

$ dc
5 3.0 / p
1

Same.

Ruby:

$ irb
> 5 / 3.0
=> 1.6666666666666667
> 5 / 3
=> 1

As you can see, Ruby does what we expect when we give it an explicit floating-point

https://ruby-doc.org/core-2.2.10/Bignum.html

https://ruby-doc.org/core-3.0.2/Rational.html

WORK IN PROGRESS