GithubHelp home page GithubHelp logo

farrel / split_into Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 228 KB

SplitInto is a micro API whose sole purpose is to split an integer into equal or roughly equal parts.

License: Other

Ruby 100.00%

split_into's Introduction

SplitInto

Overview

SplitInto is a micro API whose sole purpose is to split an integer into equal or roughly equal parts. This is useful when dealing with objects that are not well suited to being split into fractional parts.

For instance 8 cents can not be split into 3 parts equally due to the lack of 1/2 cent coins, but can be split into roughly equal parts of 2c, 3c and 3c. Using SplitInto this would be written as:

SplitInto.split(8,3) # [2, 3, 3]

9 cents can be split equally

SplitInto.split(9,3) # [3, 3, 3]

The part components will never differ by more than 1.

This was written partly to dogfood simplecheck and see what changes it requires in a real API.

Installation

gem install split_into

Usage

SplitInto can either act as a standalone module or as an extension of the Integer class.

As a standalone module:

require 'split_into'

SplitInto.split(8,4) # [2, 2, 2, 2]

To extend integer require split_into/extend_integer.

require 'split_into/extend_integer'

10.split_into(4) # [2, 2, 3, 3]

If an integer can not be split a SplitInto::SplitError exception is raised.

begin
  SplitInto.split(10,-2) 
rescue SplitInto::SplitError => exception
  puts exception.message # 'Divisor is less than zero'
end

License

SplitInto is distributed under the BSD License. See LICENSE file for details.

Copyright

2014 Aimred CC. All rights reserved.

split_into's People

Contributors

farrel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.