Sha256: b21427752f8d861d307ccfd1522f9f57e3123ca1684b75ba6b0e010cb804cfc5

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

class Xacto

  # Extensions for String class.
  # These methods are taken directly from Ruby Facets.
  #
  module String

    # Provides a margin controlled string.
    #
    #   x = %Q{
    #         | This
    #         |   is
    #         |     margin controlled!
    #         }.margin
    #
    #
    #   NOTE: This may still need a bit of tweaking.
    #
    #  CREDIT: Trans

    def margin(n=0)
      #d = /\A.*\n\s*(.)/.match( self )[1]
      #d = /\A\s*(.)/.match( self)[1] unless d
      d = ((/\A.*\n\s*(.)/.match(self)) ||
          (/\A\s*(.)/.match(self)))[1]
      return '' unless d
      if n == 0
        gsub(/\n\s*\Z/,'').gsub(/^\s*[#{d}]/, '')
      else
        gsub(/\n\s*\Z/,'').gsub(/^\s*[#{d}]/, ' ' * n)
      end
    end

    # Preserves relative tabbing.
    # The first non-empty line ends up with n spaces before nonspace.
    #
    #  CREDIT: Gavin Sinclair

    def tabto(n)
      if self =~ /^( *)\S/
        indent(n - $1.length)
      else
        self
      end
    end

    # Indent left or right by n spaces.
    # (This used to be called #tab and aliased as #indent.)
    #
    #  CREDIT: Gavin Sinclair
    #  CREDIT: Trans

    def indent(n)
      if n >= 0
        gsub(/^/, ' ' * n)
      else
        gsub(/^ {0,#{-n}}/, "")
      end
    end

  end

  class ::String #:nodoc:
    include Xacto::String
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dnote-1.6.1 lib/dnote/string.rb
dnote-1.6.0 lib/dnote/string.rb
dnote-1.4.0 lib/dnote/string.rb
dnote-1.3.1 lib/dnote/string.rb
dnote-1.3.0 lib/dnote/string.rb
dnote-1.2.1 lib/dnote/string.rb
dnote-1.2.0 lib/dnote/string.rb
dnote-1.1.4 lib/dnote/string.rb
dnote-1.1.3 lib/dnote/string.rb
dnote-1.1.2 lib/dnote/string.rb