Build Status Maintainability Inline docs

Calc

This simple Reverse Polish Notation (RPN) Calculator is built to be composable. It is a command-line program that (as of this writing) has the following features:

  • Ability to do basic arithmetic: Addition, subtraction, multiplication, and division

  • Ability to operate in console mode or take input by file

  • Support for a quit command (via q) in addition to EOF (or Ctrl-D)

  • Graceful recovery from input errors such as invalid operations (e.g., trying to perform addition with only 1 operand)

Installation

Install it locally like so:

$ hub clone gary/calc
$ cd calc
$ rake install:local

Usage

After installing it, invoke it as follows:

$ calc [optional-file]

Example Output

$ calc
> 4
4
> 3.75
3.75
> *
15
> 2
2
> /
7.5
> q

Architectural Highlights:

  • No runtime dependencies

  • Classes abide by the Single Responsibility Principle

  • All components (except the Main) strive to be loosely coupled and highly reusable

  • Public interfaces for all classes have:

    • Distinct earmarks via YARD's @api tag
    • Documentation of inputs, outputs, exceptions raised, and return values
    • Maintenance friendly signatures (i.e., exclusive use of keyword arguments)
  • All member variables accessed through attr_ macros to show intent and are grouped at the top of each class declaration

  • Calculator is built to be composable: New operations can be added with ease

  • Interface is also built with extension to new, IO-like interfaces in mind

Hindsight

If I was to spend more time on this project, I would do two things:

  • Mutation test it. I feel that I did a thorough job of designing my components to minimize surprise and control user input to provide a good user experience, but I human. Mutation testing can find additional code that is either not covered or does not have a specced side effect. Maybe next time.

  • Make its output less "echo-ey". While I do feel that the output is intelligible, I believe it could be improved to, say, only return either the sole operand (in the case of single item, non-sign input) or the result of an operation (in the case of a multi-item input that could be a complex calculation).

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gary/calc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.