Class: Tuple

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

Overview

Array where elements can be shrunk but not removed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ Tuple

Returns a new instance of Tuple.



49
50
51
52
# File 'lib/rantly/shrinks.rb', line 49

def initialize(a)
  @array = a
  @position = a.size - 1
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



82
83
84
# File 'lib/rantly/shrinks.rb', line 82

def array
  @array
end

Instance Method Details

#[](i) ⇒ Object



54
55
56
# File 'lib/rantly/shrinks.rb', line 54

def [](i)
  @array[i]
end

#[]=(i, value) ⇒ Object



58
59
60
# File 'lib/rantly/shrinks.rb', line 58

def []=(i, value)
  @array[i] = value
end

#each(&block) ⇒ Object



78
79
80
# File 'lib/rantly/shrinks.rb', line 78

def each(&block)
  @array.each(&block)
end

#inspectObject



74
75
76
# File 'lib/rantly/shrinks.rb', line 74

def inspect
  to_s
end

#lengthObject



62
63
64
# File 'lib/rantly/shrinks.rb', line 62

def length
  @array.length
end

#retry?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rantly/shrinks.rb', line 99

def retry?
  @position >= 0
end

#shrinkObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rantly/shrinks.rb', line 84

def shrink
  shrunk = @array.dup
  while @position >= 0
    e = @array.at(@position)
    break if e.respond_to?(:shrinkable?) && e.shrinkable?

    @position -= 1
  end
  if @position >= 0
    shrunk[@position] = e.shrink
    @position -= 1
  end
  Tuple.new(shrunk)
end

#shrinkable?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/rantly/shrinks.rb', line 103

def shrinkable?
  @array.any? { |e| e.respond_to?(:shrinkable?) && e.shrinkable? }
end

#sizeObject



66
67
68
# File 'lib/rantly/shrinks.rb', line 66

def size
  length
end

#to_sObject



70
71
72
# File 'lib/rantly/shrinks.rb', line 70

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