Class: Deflating
- Inherits:
-
Object
- Object
- Deflating
- Defined in:
- lib/rantly/shrinks.rb
Overview
Array where the elements that can’t be shrunk are removed
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
Instance Method Summary collapse
- #[](i) ⇒ Object
- #[]=(i, value) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(a) ⇒ Deflating
constructor
A new instance of Deflating.
- #inspect ⇒ Object
- #length ⇒ Object
- #retry? ⇒ Boolean
- #shrink ⇒ Object
- #shrinkable? ⇒ Boolean
- #size ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(a) ⇒ Deflating
Returns a new instance of Deflating.
110 111 112 113 |
# File 'lib/rantly/shrinks.rb', line 110 def initialize(a) @array = a @position = a.size - 1 end |
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
143 144 145 |
# File 'lib/rantly/shrinks.rb', line 143 def array @array end |
Instance Method Details
#[](i) ⇒ Object
115 116 117 |
# File 'lib/rantly/shrinks.rb', line 115 def [](i) @array[i] end |
#[]=(i, value) ⇒ Object
119 120 121 |
# File 'lib/rantly/shrinks.rb', line 119 def []=(i, value) @array[i] = value end |
#each(&block) ⇒ Object
139 140 141 |
# File 'lib/rantly/shrinks.rb', line 139 def each(&block) @array.each(&block) end |
#inspect ⇒ Object
135 136 137 |
# File 'lib/rantly/shrinks.rb', line 135 def inspect to_s end |
#length ⇒ Object
123 124 125 |
# File 'lib/rantly/shrinks.rb', line 123 def length @array.length end |
#retry? ⇒ Boolean
159 160 161 |
# File 'lib/rantly/shrinks.rb', line 159 def retry? @position >= 0 end |
#shrink ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rantly/shrinks.rb', line 145 def shrink shrunk = @array.dup if @position >= 0 e = @array.at(@position) if e.respond_to?(:shrinkable?) && e.shrinkable? shrunk[@position] = e.shrink else shrunk.delete_at(@position) end @position -= 1 end Deflating.new(shrunk) end |
#shrinkable? ⇒ Boolean
163 164 165 |
# File 'lib/rantly/shrinks.rb', line 163 def shrinkable? !@array.empty? end |
#size ⇒ Object
127 128 129 |
# File 'lib/rantly/shrinks.rb', line 127 def size length end |
#to_s ⇒ Object
131 132 133 |
# File 'lib/rantly/shrinks.rb', line 131 def to_s @array.to_s.insert(1, 'D ') end |