circular_array 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -4
- data/lib/circular_array.rb +3 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89e06c14c6a321cdbed279de04d4bccdd018f5a16ec804b5b8253db4584f2117
|
4
|
+
data.tar.gz: 05b01f1c0c0e79c77e04f76770d927050ff7a7a7f64329885757262ba91b2503
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b586230519054f33154e22b933b6c03cc1a81717d815869b8a6fd917c0c04da2b7efd1ccfcb3ffa28b2216af572ed88bc47e1859db6b673d0cc2d6f15cb3dd2d
|
7
|
+
data.tar.gz: b1dfd513506b1546291a4634d2fc539bb069dd9445e7efdcf30b5d629caada23a0280dee19ed36b450c2ec7a56d6c5c32cec8b15122cfbfdc00bdd50c945361f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# CircularArray
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.com/sveredyuk/ruby-circular-array.svg?branch=master)](https://travis-ci.com/sveredyuk/ruby-circular-array)
|
4
|
+
|
5
|
+
You have a week with days and you want to iterate over the week and never met `nil`, but:
|
4
6
|
```ruby
|
5
7
|
week = [:mon, :tue, :wed, :thu, :fri, :sat, :sun]
|
6
8
|
week[0] # => :mon
|
7
9
|
week[3] # => :thu
|
8
10
|
week[6] # => :sun
|
9
|
-
week[7] # => nil... stop
|
11
|
+
week[7] # => nil... stop what?! I need monday!
|
10
12
|
```
|
11
13
|
|
12
14
|
But it's possible with `CircularArray`
|
@@ -33,7 +35,7 @@ circular_week[14] # => :mon
|
|
33
35
|
# great!
|
34
36
|
```
|
35
37
|
|
36
|
-
**You can use it for
|
38
|
+
**You can use it for recursive matching, but do not forget to add anti-infinity loop clause**
|
37
39
|
|
38
40
|
Only for empty collection in returns `nil`
|
39
41
|
```ruby
|
@@ -45,7 +47,7 @@ empty_circular_array[1] # => nil
|
|
45
47
|
|
46
48
|
1. Matching over a list of possible solutions
|
47
49
|
2. Cycling dates for complex delivery logic
|
48
|
-
3.
|
50
|
+
3. ...
|
49
51
|
|
50
52
|
|
51
53
|
## Installation
|
data/lib/circular_array.rb
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class CircularArray < Array
|
4
|
-
VERSION = '0.
|
4
|
+
VERSION = '0.2.0'
|
5
5
|
|
6
6
|
def [](index)
|
7
|
-
|
7
|
+
return nil if empty?
|
8
8
|
|
9
|
-
|
10
|
-
return nil if size.zero?
|
11
|
-
|
12
|
-
self[index - size]
|
9
|
+
super(index % size)
|
13
10
|
end
|
14
11
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: circular_array
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Volodya Sveredyuk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|