ahora suena
r/o

suena / sizeup.rb

1
MAX_LINE_LEN = 18
2
3
def size_up(text)
4
lines = 1
5
ix = 0
6
remaining = text.length
7
8
while remaining > MAX_LINE_LEN
9
s = ix + MAX_LINE_LEN - 1
10
found = false
11
while s >= ix
12
if text[s] == ' '
13
puts text[ix...s]
14
lines += 1
15
ix = s + 1
16
remaining = text.length - ix
17
found = true
18
break
19
end
20
s -= 1
21
end
22
if !found
23
lines += 1
24
puts text[ix...ix + MAX_LINE_LEN]
25
ix += MAX_LINE_LEN
26
remaining -= MAX_LINE_LEN
27
end
28
end
29
30
if remaining > 0
31
puts text[ix..]
32
end
33
34
p lines
35
end
36
37
puts "123456789012345678"
38
puts "=================="
39
size_up("123456789012345678")
40
size_up("12345678901234567890")
41
size_up("123456789012345 6789")
42
size_up("STREAM PALETTE 5 -RANKED-")
43
size_up("hello my cookie wookie")
44
size_up("nyonkers")
45
size_up("whackydackysackynacky")
46