0.0
No release in over 3 years
Low commit activity in last 3 years
Simple gem used for merging range-like objects. If you have problems with operations on ranges this gem can save you some time.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.6.4
>= 0
~> 2.3.0
 Project Readme

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]]

Dividing with equal interval¶ ↑

r = RangesMerger.new

r += [[1, 9]] # store 1 as lowest value

r -= [[1, 2], [4, 5]]

result = r % 2

# result => [[2, 4], [5, 7], [7, 9]]

result = r / 2

# result => [[2, 4], [5, 7], [7, 9]]

result = r.divide_eqi(2)

# result => [[5, 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 :]

Calculation of energy (beta)¶ ↑

Imagine you have information about voltage and current and you want to calculate work/energy. These measurements are not set to one point in time but to a time range (‘from’ and ‘to’). These time ranges are not identical for voltage and current. So what would you do? You can use this gem! :)

# currents = [
#   [ time_from, time_to, value ],
#   ...
# ]
#

currents = [
  [1, 2, 1],
  [2, 3, 2]
]

voltages = [
  [1, 2, 2],
  [2, 3, 1]
]

result = RangesMerger.energy_calculation(voltages, currents)

# result => 4

This is an early version, it should work and calculate correct amount of energy, but it could not :].

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.