conv 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/conv.gemspec +2 -0
  3. data/lib/array.rb +8 -0
  4. data/lib/loop.rb +72 -0
  5. metadata +3 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/conv.gemspec CHANGED
@@ -25,6 +25,8 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "conv.gemspec",
27
27
  "lib/conv.rb",
28
+ "lib/array.rb",
29
+ "lib/loop.rb",
28
30
  "test/helper.rb",
29
31
  "test/test_array.rb",
30
32
  "test/test_loop.rb"
data/lib/array.rb ADDED
@@ -0,0 +1,8 @@
1
+ class Array
2
+ # => rand
3
+ # returns a random element from the array
4
+ # by calling Array's private rand method.
5
+ def rand
6
+ self.rand
7
+ end
8
+ end
data/lib/loop.rb ADDED
@@ -0,0 +1,72 @@
1
+ module Loopable
2
+ # Mixin for Array like objects
3
+
4
+ #set the position
5
+ def set_position(index)
6
+ @position = index
7
+ end
8
+ alias_method :set_pos, :set_position
9
+
10
+ #returns the position ( current index)
11
+ def position
12
+ unless(defined?(@position)) then
13
+ @position = 0
14
+ end
15
+ @position
16
+ end
17
+ alias_method :current_index, :position
18
+ alias_method :pointer, :pointer
19
+
20
+ #retruns the object at position
21
+ def current
22
+ #if self[position].respond_to?(:loopable_current) then
23
+ self[position].loopable_current if self[position].respond_to?('loopable_current')
24
+ #end
25
+ self[position]
26
+ end
27
+
28
+ #increments position, looping back to begining
29
+ #if necessary
30
+ # returns the object at the new position
31
+ def next
32
+ if(position == self.size - 1) then
33
+ @position = 0
34
+ else
35
+ @position = @position + 1
36
+ end
37
+ current
38
+ end
39
+
40
+
41
+
42
+ #moves the position forward a number of steps and
43
+ #returns the object stored there
44
+ def forward(steps = 1)
45
+ steps.times { self.next }
46
+ current
47
+ end
48
+
49
+ # decrements position, looping back from the end
50
+ #if necessary
51
+ def prev
52
+ if(position == 0) then
53
+ @position = self.size - 1
54
+ else
55
+ @position = @position - 1
56
+ end
57
+ current
58
+ end
59
+
60
+ #moves the position back a number of steps and
61
+ #returns the object stored there
62
+ def backward(steps = 1)
63
+ steps.times { self.prev }
64
+ current
65
+ end
66
+
67
+ end # => module Loop
68
+
69
+ class Loop < Array
70
+ include Loopable
71
+ end
72
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - codesponge
@@ -40,6 +40,8 @@ files:
40
40
  - VERSION
41
41
  - conv.gemspec
42
42
  - lib/conv.rb
43
+ - lib/array.rb
44
+ - lib/loop.rb
43
45
  - test/helper.rb
44
46
  - test/test_array.rb
45
47
  - test/test_loop.rb