class String

Add methods to classes related to the character encoding/multilingualization features present in 1.9 that are not in 1.8.

Slightly extend the builtin string class to format help text messages.

Public Instance Methods

decode_qp() click to toggle source

Unfortunately, ruby's quoted-printable decoder (unpack("M")) isn't liberal enough. (It stops decoding when it hits the first invalid qp encoding sequence e.g. '=9'). This code was ripped off from perl's MIME module. (Copyright 1995-1997 Gisle Aas.)

# File buryspam.rb, line 414
def decode_qp
  # http://rfc.sunsite.dk/rfc/rfc2045.html
  # rule #3 (trailing space must be deleted)
  gsub(/[ \t]+?(\r?\n)/,'\1').
  # rule #5 (soft line breaks)
  gsub(/=\r?\n/, "").
  gsub(/=([\da-fA-F]{2})/) { $1.hex.chr(Encoding::BINARY) }
end
force_encoding(encoding) click to toggle source
# File buryspam.rb, line 59
def force_encoding(encoding)
  # Ignore encoding parameter.
  self
end
format(opts = {}) click to toggle source

Format strings for display:

  • When formatting a string to non-HTML, remove all pairs of matching HTML tags replace entities with appropriate characters, and remove links signified by ....

  • When formatting a string to HTML, replace links with appropriate href anchors, set indented lines of code to an appropriate font and add <br/> tags as necessary.

Word wrapping is done in both cases. Supported opts values:

  • :length: the maximum line length for word wrapping (default 78)

  • :html: boolean indicating whether we are formatting for HTML (true) or non-HTML (false) output (default false)

# File buryspam.rb, line 339
def format(opts = {})
  params = { :length => 78, :html => false }.merge(opts)
  lines = [""]
  each_line { |line|
    if params[:html]
      line.gsub!(/\b_(\S+)_\b/) {
        "<a href=\"##{$1}\"><tt>#{$1}</tt></a>"
      }
    else
      line = line.gsub(/<(\S+).*?>(.*?)<\/\11>>/, '\2').
                  gsub(/&mdash;/, "---").
                  gsub(/&lt;/, "<").
                  gsub(/&gt;/, ">").
                  gsub(/\b_(\S+)_\b/, '\1')
    end
    line.chomp!
    case line
      when /^  /      # Indented verbatim text (e.g. code)
        if params[:html]
          lines.last << "<br/>"
          line.gsub!(/^\s\s/, "&nbsp;&nbsp;")
          lines << "<tt>#{line}</tt><br/>" << ""
        else
          lines << "#{line}" << ""
        end
      when ""         # Blank line.
        if params[:html]
          lines.last << "<br/>"
          lines << "<br/>" unless lines.last == "<br/>"
        else
          lines << "" unless lines.last.empty?
        end
        lines << ""
      else            # Regular line.  Do word wrapping.
        words = line.split(/(\s+)/)
        words.each { |word|
          if lines.last.size + word.size < params[:length]
            lines.last << word
          else
            # Remove trailing blanks from previous line then
            # start a new indented line.
            lines.last.strip
            lines << " " + word unless word.strip.empty?
          end
        }
    end
  }
  # Remove trailing blank line.
  lines.pop if lines.last.empty?
  # Add newlines to all lines except last.
  lines.map! { |l| l + "\n" }
  lines.last.chomp!

  lines.join("")
end
pluralize(num, singular = "", plural = "s") click to toggle source

Assuming self is a formatted string, pluralize the string using the given number. e.g.:

"%d item%s".pluralize(item_count)

singular is substituted for %s if num is one. plural is substituted otherwise.

# File buryspam.rb, line 400
def pluralize(num, singular = "", plural = "s")
  self % [num, (num == 1 ? singular : plural)]
end
yes?() click to toggle source

Return true if we match "yes". Used for confirming interactive requests to continue.

# File buryspam.rb, line 406
def yes?
  /^y(es)?$/.match(self)
end