Skip to content

A gem that adds addition and subtraction to Ruby's Range class.

License

Notifications You must be signed in to change notification settings

stijnjanmaat/range_operators

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A gem that adds addition and subtraction to Ruby's Range class.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%