def wrap(num_lines)
      cleaned = gsub(/\s+/, ' ').strip

      chars_per_line = cleaned.size / num_lines.to_f

      lines = []
      cleaned.split.each do |word|
        if lines.empty?
          lines << word
        else
          if (lines[-1].size + 1 + word.size) <= chars_per_line ||
              lines.size >= num_lines
            lines[-1] << ' ' unless lines[-1].empty?
            lines[-1] << word
          else
            lines << word
          end
        end
      end

      Caption.new(lines.join("\n"))
    end
def wrap(num_lines)
      cleaned = gsub(/\s+/, ' ').strip

      chars_per_line = cleaned.size / num_lines.to_f

      lines = []
      cleaned.split.each do |word|
        if lines.empty?
          lines << word
        else
          if (lines[-1].size + 1 + word.size) <= chars_per_line ||
              lines.size >= num_lines
            lines[-1] << ' ' unless lines[-1].empty?
            lines[-1] << word
          else
            lines << word
          end
        end
      end

      Caption.new(lines.join("\n"))
    end
def wrap ( num_lines ) cleaned = gsub ( / \s / , ' ' ) . strip chars_per_line = cleaned . size / num_lines . to_f lines = [ ] cleaned . split . each do | word | if lines . empty? lines << word else if ( lines [ - 1 ] . size + 1 + word . size ) <= chars_per_line || lines . size >= num_lines lines [ - 1 ] << ' ' unless lines [ - 1 ] . empty? lines [ - 1 ] << word else lines << word end end end Caption . new ( lines . join ( "\n" ) ) end
Wrap the string of into num_lines lines.
Wrap the string of into num_lines lines .