module ParagraphFormat
Public Instance Methods
Source
# File lib/paragraph_format.rb, line 6 def build_paragraphs(items, max_bytes:, separator: ", ") entries = Array(items).map(&:to_s).map(&:strip).reject(&:empty?) return [] if entries.empty? paragraphs = [] current = +"" entries.each do |entry| candidate = current.empty? ? entry : "#{current}#{separator}#{entry}" if candidate.bytesize > max_bytes && !current.empty? paragraphs << current current = entry.dup else current = candidate end end paragraphs << current unless current.empty? paragraphs end