Skip to content

Instantly share code, notes, and snippets.

@907th
Created August 27, 2015 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 907th/83dcd661429425dd90e5 to your computer and use it in GitHub Desktop.
Save 907th/83dcd661429425dd90e5 to your computer and use it in GitHub Desktop.
Hey, ya! Use recursion!
# 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
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