What's the simplest way to limit an array of words such that the resulting array, when the words are joined, is less than 40 characters? Something that acts like this:
words = ["ruby", "rails", "jquery", "javascript", "html"]
words.join.length #=> 29
words.join(", ").length #=> 37
words = ["ruby", "rails", "jquery", "javascript", "html", "css", "api"]
words.join.length #=> 35
words.join(", ").length #=> 47
words.chunk(:max_chars => 40) #=> ["ruby", "rails", "jquery", "javascript", "html"]
Thanks!
Update
Got this so far:
def chop(array, separator = "", max = 40)
result = []
array.each do |word|
break if (result.join(separator).length + word.length > max)
result << word
result
end
result
end