Skip to content

A simple program built for the terminal that takes in a string mathematical expression and spits out the output as a mixed fraction using the string format.

Notifications You must be signed in to change notification settings

rookim/ruby-string-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Ruby Terminal String Calculator

Overview

This simple program was inspired by an online assessment I took. It takes in a string input (a mathematical expression) from the user via the gets.chomp() method and calculates it. The output depends on what the expression is (mixed fraction, fraction, integer), but it is in a string format.

There are some rules!

  1. There must be a space between digits and mathematical symbols. For example, a user would enter "1 + 1" and not "1+1".
  2. Mixed fractions must be entered using "_" to separate the whole number from fraction. For example, 1_3/4, 15_1/2.
  3. You do not need to add quotation marks when entering your mathematical expression. The gets.chomp() method only takes in strings.

Through this program, I learned about Ruby Rational class! Rationals are yet another object available in the Ruby programming language. Rationals are basically what most of us know as fractions and offer a way of representing exact numbers without any rounding errors.

A real-life scenario that rationals can be very useful in is of course this calculator program I built. Another use-case is in baking! You would definitely NOT say 0.5214667 cups of flour, would you? That would certainly be ridiculous!

How To Use

If you would like, you are more than welcome to fork this repository and test it out in your personal terminal!

Demo

For your convenience, I've embedded a video demo of my string calculator in action! Take a look :)

ruby_terminal_string_calculator_original.mp4

Source: https://vimeo.com/702616598/f449552d8d

About

A simple program built for the terminal that takes in a string mathematical expression and spits out the output as a mixed fraction using the string format.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages