[Rubygems-developers] Pretty Listings for RubyGems

Chad Fowler chad at chadfowler.com
Sat Mar 20 12:12:43 EST 2004


This one is also applied.


Chad




On Mar 20, 2004, at 11:06 AM, Gavin Sinclair wrote:

> +#
> +# These implementations are not idiot-proof or intended for general 
> use.  They are to support
> +# the pretty-printing of lists of gems.
> +#
> +class String # :nodoc:
> +  # Wrap string to +n+ characters, inserting newlines and ensuring 
> words are not broken.
> +  # Stolen from 'ri'.
> +  def wrap(n)
> +    result = []
> +    pattern = Regexp.new("^(.{0,#{n}})[ \n]")
> +    work = self.dup
> +    while work.length > n
> +      if work =~ pattern
> +        result << $1
> +        work.slice!(0, $&.length)
> +      else
> +        result << work.slice!(0, n)
> +      end
> +    end
> +    result << work if work.length.nonzero?
> +    result.join("\n")
> +  end
> +
> +  # Indent string by +n+ spaces.
> +  def indent(n)
> +    gsub(/^/, " " * n)
> +  end
> +end



More information about the Rubygems-developers mailing list