Giter Site home page Giter Site logo

range_operators's Introduction

RangeOperators

This gem will mixin range operations into Ruby’s Range and Array classes.

Range Methods:: Addition and subtraction

Array Methods

Combining elements into ranges, determining intersection of elements,

and determining missing elements

It assumes inclusive ranges (ie. 1..4) and range.first <= range.last. Also, the objects making up the ranges must have #succ and #<=> methods.

Usage

The methods added are Range#- (alias Range#minus), Range#+ (alias Range#plus), Array#rangify, Array#intersection, and Array#missing.

Examples:

irb > require ‘range_operators’

irb > (1..10) - (4..6) => [1..3, 7..10]

irb > (1..10).minus(9..12) => [1..8]

Note: #- requires that the second operand have a #- (integer) method defined (ie. Fixnum, Bignum, and Date). (This allows the determination of a previous/predecessor value of an object.)


irb > (1..10) + (9..12) => [1..12]

irb > (1..10).plus(15..20) => [1..10, 15..20]


irb > [1,2,3,6,7,8].rangify => [1..3, 6..8]

irb > [10..15, 16..20, 21, 22].rangify => [10..22]


irb > [1, 2].intersection => nil

irb > [1..10, 1].intersection => 1

irb > [5..10, 1..10, 4..8 ].intersection => 5..8

Note: #intersection will determine the values in common to all of the elements.


irb > [100, 9..11, 14, 1..5, 16, 10..12, 17..17].missing => [6..8, 13, 15, 18..99]

Note: Like #-, #missing requires that the second operand have a #- (integer) method.

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.