bin/wrap.rb
##
# A version of String#wrap that produces short (less
# lines) and balanced (similar line widths) output.
#
A version of String#wrap that produces short (less
lines) and balanced (similar line widths) output.
A version of String#wrap that produces short ( less lines ) and balanced ( similar line widths ) output .
def balanced_wrap width
    results   = {}
    width.downto(((width/2.5).floor)) do |width|
      lines            = wrap(width)
      height           = lines.length
      widths           = lines.map {|s| s.length}
      penalty          = (height * widths.max) + widths.stdev
      results[penalty] = lines
      if $DEBUG
        p :width => width, :height => height, :penalty => penalty
        puts '@' * width, widths.map {|w| 'X' * w}
      end
    end
    results[results.keys.min]
  end
def balanced_wrap width results = { } width . downto ( ( ( width / 2.5 ) . floor ) ) do | width | lines = wrap ( width ) height = lines . length widths = lines . map { | s | s . length } penalty = ( height * widths . max ) + widths . stdev results [ penalty ] = lines if $DEBUG p :width => width , :height => height , :penalty => penalty puts '@' * width , widths . map { | w | 'X' * w } end end results [ results . keys . min ] end
A version of String#wrap that produces short (less
lines) and balanced (similar line widths) output.
A version of String#wrap that produces short ( less lines ) and balanced ( similar line widths ) output .
# max_height = (length / width.to_f).ceil # max_height += 1 if max_height > 2 # break if height > max_height
width
<not_specific>