ranges_merger

Very simple gem for merging range-like objects.

How to use, simple merge

If you want to merge please use code just like below.

a = [1, 3]

b = [2, 4]

c = [4, 6]

result = RangesMerger.merge([a, b, c])

# result => [[1, 6]]

Range objects

If you want to merge Range objects.

ranges = [

     (1..2),

     (2..3),

     (5..8)

]

result = RangesMerger.merge(ranges)

# result => [(1..3),(5..8)]

Exclusion

If you want to exclude please use code just like below.

First - base:

a = [1, 3]

b = [3, 5]

c = [9, 11]

Seconds - exclusions:

x = [2, 4]

y = [4, 10]

And run some processing

result = RangesMerger.exclude([a, b, c], [x, y])

# result => [[1, 2], [10, 11]]

I want an instance

If you want to have instance, add and remove ranges as you wish just do something like that:

r = RangesMerger.new

r += [[1, 10]]

r += [[8, 15]]

# r.to_array => [[1, 15]]

r -= [[2, 4]]

r -= [[11, 12]]

# r.to_array => [[1, 2], [4,11], [12,15]]

Dividing

Then you have range you may divide it to small pieced with proper interval. You can divide it with ‘partials’ - smaller parts than interval, or without them.

r = RangesMerger.new

r += [[1, 9]]

result = r % 3

# result => [[1, 4], [4, 7]]

result = r / 3

# result => [[1, 4], [4, 7], [7, 9]]

Ranges are not only numbers

They can be made from all objects which can be compared, and has simple math. operation like sum. For example - Time object.

t_now = Time.now

r = RangesMerger.new

r += [[t_now - 2*3600, t_now - 0*3600]]
r += [[t_now - 3*3600, t_now - 1.3600]]

result = r.to_array

# try by yourself or read spec :]

r -= [[t_now - 2*3600, t_now - 1*3600]]

result = r.to_array

# try by yourself or read spec :]

result = r / (10*60)

# try by yourself or read spec :]

Contributing to ranges_merger

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Aleksander Kwiatkowski. See LICENSE.txt for further details.