Class: Counterstring::String
- Inherits:
-
Struct
- Object
- Struct
- Counterstring::String
- Defined in:
- lib/counterstring.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
Returns the value of attribute length.
Instance Method Summary collapse
Instance Attribute Details
#length ⇒ Object
Returns the value of attribute length
8 9 10 |
# File 'lib/counterstring.rb', line 8 def length @length end |
Instance Method Details
#to_s ⇒ Object
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 |