Sha256: 212f8eaed51b139965cbe63ccf9927b9683bf7283ad66df01803d244af5db7eb

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require 'cgi'
require 'iconv'
require 'module_extensions'

# == Synopsis
# Various extensions to the String class
# Note, uses the Module.my_extension method to only add the method if
# it doesn't already exist.
class String
  my_extension("unescape_html") do
    # == Synopsis
    # unescape HTML
    def unescape_html
      Iconv.conv("UTF-8", 'ISO-8859-1', CGI::unescapeHTML(self))
    end
  end

  my_extension("escape_unicode") do
    # == Synopsis
    # this handles unicode characters by converting each byte to "%XX"
    # where XX is the hex value
    def escape_unicode
      self.each_byte.collect{|c| c.to_i > 127 ? "%#{c.to_i.to_s(16)}" : c.chr}.join('')
    end
  end

  my_extension("strip_tags") do
    # == Synopsis
    # remove angle bracket tags from the string
    def strip_tags
      gsub(/<\/?[^>]*>/, "")
    end
  end

  my_extension("ext") do
    # == Synopsis
    # Replace the file extension with +newext+.  If there is no extenson on
    # the string, append the new extension to the end.  If the new extension
    # is not given, or is the empty string, remove any existing extension.
    #
    # +ext+ is a user added method for the String class.
    def ext(newext='')
      return self.dup if ['.', '..'].include? self
      if newext != ''
        newext = (newext =~ /^\./) ? newext : ("." + newext)
      end
      dup.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || self + newext
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
royw-roys_extensions-0.0.2 lib/string_extensions.rb