Class: Counterstring::String

Inherits:
Struct
  • Object
show all
Defined in:
lib/counterstring.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lengthObject

Returns the value of attribute length

Returns:

  • (Object)

    the current value of length



8
9
10
# File 'lib/counterstring.rb', line 8

def length
  @length
end

Instance Method Details

#to_sObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/counterstring.rb', line 9

def to_s
  return '' if length.zero?

  return length.to_s if length == 1

  output = '1' + marker
  last_position_number = 1

  while output.size < length
    position_number = output.size + (last_position_number.to_s.size)
    if position_number.to_s.size > last_position_number.to_s.size
      position_number += 1
    end
    token = position_number.to_s + marker
    remaining_length = length - output.size
    output += token
    last_position_number = position_number
  end

  output[0,length]
end