• Stars
    star
    2
  • Language
    Crystal
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Range object operation in Crystal

ranger

ranger is a Crystal library to operate on Range objects in Crystal programming language. Current version can include, exclude and two types of split Range object.

First split you choose how many smaller ranges would you like, the second type allow you to specify range length. It is very useful for Time related operations.

Installation

Add this to your application's shard.yml:

dependencies:
  ranger:
    github: akwiatkowski/ranger

Usage

Include simple ranges

require "ranger"

result = (1..2) + (2..3)
puts result.inspect
# [1..3]

Keep in mind that you can only include two Range objects. This is because result is Array(Range). If you include two exclusive Range the result are two Range objects. That means result of inclusion cannot be one Range.

Include more than 2 ranges

In case you want to include more objects please use example below.

require "ranger"

r = Ranger.new(1..2)
r.include(2..3)
r.include(3..4)

result = r.render
puts result.inspect
# [1..4]

Exclude ranges

Exclusion is opposite operation to inclusion. This library allow you to do it.

require "ranger"

result = (1..10) - (5..6)
puts result.inspect
# [1..5, 6..10]

Exclude more ranges

require "ranger"

r = Ranger.new(10..20)
r.exclude(15..16)
r.exclude(18..19)

result = r.render
puts result.inspect
# [10..15, 16..18, 19..20]

Divide/split Range into more objects

You can use ranger to split one Range object into more smaller objects.

result = (0..10) / 2
puts result.inspect
# [0..5, 5..10]

Divide/split Range into specific ranges

If you have 1 month Time Range and would like to divide into 1-week Range objects you can use modulo operator - %.

ra = (Time.new(2016, 1, 1))..(Time.new(2016, 2, 1))
j = Time.new(2016, 1, 8) - Time.new(2016, 1, 1)
result = ra % j

Keep in mind last Range object will be not full 1-week range. If you would like only full 1-week ranges you could change operator to bitwise xor - ^. Sorry, there is lack of proper operators.

ra = (Time.new(2016, 1, 1))..(Time.new(2016, 2, 1))
j = Time.new(2016, 1, 8) - Time.new(2016, 1, 1)
result = ra ^ j

Alternatively there is method div.

ra = (Time.new(2016, 1, 1))..(Time.new(2016, 2, 1))
j = Time.new(2016, 1, 8) - Time.new(2016, 1, 1)
result = ra.div(j, allow_partial: false)

Development

TODO: Write development instructions here

Contributing

  1. Fork it ( https://github.com/akwiatkowski/ranger/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

More Repositories

1

crystal_api

Simple PostgreSQL REST API in Crystal with devise-like auth.
Crystal
68
star
2

kemal-auth-token

Kemal middleware to authentication via HTTP header token using JWT
Crystal
30
star
3

tremolite

Blog generator
Crystal
20
star
4

gpx2exif

A script to geotag your photos using GPX files
Ruby
14
star
5

technical_graph

Simple technical graphs for Ruby
Ruby
10
star
6

ocranizer

Simple CLI organizer
Crystal
7
star
7

i_hope_you_die

eWuล›
Ruby
4
star
8

akwiatkowski.github.com

Crystal
4
star
9

HomeIO

Home control system
Ruby
4
star
10

tremolite_example

Crystal
3
star
11

crystal_gpx

GPX parser in Crystal + some various utils
Crystal
3
star
12

gpx2png

Convert route storen in GPX format to map using Openstreetmap or OSM compatible tileset.
Ruby
3
star
13

ruby_city

City simulator
Ruby
2
star
14

ranges_merger

Simple gem for merging various range-like data
Ruby
2
star
15

xmpp_chat_bot

Simple bot for xmpp chat. At the moment it get titles of urls.
Ruby
2
star
16

waypoint_manager

Simple rails app to manage all waypoints, pois, created in (or for) gps device
Ruby
2
star
17

gpx_utils

Basic utils: track import, waypoint list import/export.
Ruby
2
star
18

crystal_api_sample

Sample project using crystal_api shard
Crystal
1
star
19

herealive

JavaScript
1
star
20

pizza_accounting

Simple accounting for office liabilities, for example when buying pizzas.
Ruby
1
star
21

crystal_weather

Weather fetcher in crystal
Crystal
1
star
22

bobik_utils

Various helpful scripts
Ruby
1
star
23

crystal_metar_parser

Parse METAR weather string. METAR standard is used by every major airport.
Crystal
1
star
24

weather_fetcher

Gem for fetching weather from various services
Ruby
1
star
25

meas_receiver

Library for receiving measurements from HomeIO/IoServer
Ruby
1
star
26

homeio_lite

Ruby
1
star
27

simple_metar_parser

Parsing METARs, simple way.
Ruby
1
star
28

homeio_backend

C++
1
star
29

SimplePowerStabilizer

Simple power stabilizer to convert 40-60V to 36V, with RS-485 communication using special AVR uC.
C
1
star
30

webcam_downloader

Download webcams.
Ruby
1
star
31

koko_rails

Adds Jarzฤ™bina's power to your rails app.
Ruby
1
star
32

simple_show_helper

Simple helper for 'show' pages to help lazy developers.
Ruby
1
star