Class: Deflating

Inherits:
Object
  • Object
show all
Defined in:
lib/rantly/shrinks.rb

Overview

Array where the elements that can’t be shrunk are removed

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#arrayObject (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

#inspectObject



135
136
137
# File 'lib/rantly/shrinks.rb', line 135

def inspect
  to_s
end

#lengthObject



123
124
125
# File 'lib/rantly/shrinks.rb', line 123

def length
  @array.length
end

#retry?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/rantly/shrinks.rb', line 159

def retry?
  @position >= 0
end

#shrinkObject



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

Returns:

  • (Boolean)


163
164
165
# File 'lib/rantly/shrinks.rb', line 163

def shrinkable?
  !@array.empty?
end

#sizeObject



127
128
129
# File 'lib/rantly/shrinks.rb', line 127

def size
  length
end

#to_sObject



131
132
133
# File 'lib/rantly/shrinks.rb', line 131

def to_s
  @array.to_s.insert(1, 'D ')
end