Created
August 27, 2015 12:11
-
-
Save 907th/83dcd661429425dd90e5 to your computer and use it in GitHub Desktop.
Hey, ya! Use recursion!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Split long `address_line` line into array of lines | |
# each with length not greater than `max_length` | |
def divide_address(address_line, max_length) | |
assert max_length > 0, "max_length should be a positive number" | |
address_line = address_line.strip | |
if address_line.length <= max_length | |
return [address_line] | |
else | |
head = address_line.truncate(max_length, separator: " ", omission: "") | |
tail = address_line[head.length..-1] | |
rest = divide_address(tail, max_length) | |
return [head].concat(rest) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def divide_address(address_line, line_length) | |
line_1 = truncate(address_line, length: line_length, separator: ' ', omission: '') | |
tail = address_line[line_1.length, line_length * 2] | |
line_2 = truncate(tail, length: line_length, separator: ' ', omission: '') | |
tail = tail[line_2.length, line_length] | |
line_3 = truncate(tail, length: line_length, separator: ' ', omission: '') | |
[line_1, line_2, line_3] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment