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 Method Summary collapse
- #[](i) ⇒ Object
- #[]=(i, value) ⇒ Object
- #array ⇒ 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.
113 114 115 116 |
# File 'lib/rantly/shrinks.rb', line 113 def initialize(a) @array = a @position = a.size - 1 end |
Instance Method Details
#[](i) ⇒ Object
118 119 120 |
# File 'lib/rantly/shrinks.rb', line 118 def [](i) @array[i] end |
#[]=(i, value) ⇒ Object
122 123 124 |
# File 'lib/rantly/shrinks.rb', line 122 def []=(i, value) @array[i] = value end |
#array ⇒ Object
146 147 148 |
# File 'lib/rantly/shrinks.rb', line 146 def array return @array end |
#each(&block) ⇒ Object
142 143 144 |
# File 'lib/rantly/shrinks.rb', line 142 def each(&block) @array.each(&block) end |
#inspect ⇒ Object
138 139 140 |
# File 'lib/rantly/shrinks.rb', line 138 def inspect self.to_s end |
#length ⇒ Object
126 127 128 |
# File 'lib/rantly/shrinks.rb', line 126 def length @array.length end |
#retry? ⇒ Boolean
164 165 166 |
# File 'lib/rantly/shrinks.rb', line 164 def retry? @position >= 0 end |
#shrink ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rantly/shrinks.rb', line 150 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 return Deflating.new(shrunk) end |
#shrinkable? ⇒ Boolean
168 169 170 |
# File 'lib/rantly/shrinks.rb', line 168 def shrinkable? !@array.empty? end |
#size ⇒ Object
130 131 132 |
# File 'lib/rantly/shrinks.rb', line 130 def size self.length end |
#to_s ⇒ Object
134 135 136 |
# File 'lib/rantly/shrinks.rb', line 134 def to_s @array.to_s.insert(1, "D ") end |