Class String

  1. lib/stella/core_ext.rb
Parent: Object

Assumes Time::Units and Numeric mixins are available.

Public instance methods

encode_fix (enc="UTF-8")
[show source]
# File lib/stella/core_ext.rb, line 125
  def encode_fix(enc="UTF-8")
    if RUBY_VERSION >= "1.9"
      begin
        encode!(enc, :undef => :replace, :invalid => :replace, :replace => '?')
      rescue Encoding::CompatibilityError
        BS.info "String#encode_fix: resorting to US-ASCII"
        encode!("US-ASCII", :undef => :replace, :invalid => :replace, :replace => '?')
      end
    end
    self
  end
in_seconds ()
[show source]
# File lib/stella/core_ext.rb, line 115
  def in_seconds
    # "60m" => ["60", "m"]
    q,u = self.scan(/([\d\.]+)([s,m,h])?/).flatten
    q &&= q.to_f and u ||= 's'
    q &&= q.in_seconds(u)
  end
linkify ()
[show source]
# File lib/stella/core_ext.rb, line 166
  def linkify
     self.dup.linkify!
  end
linkify! ()
[show source]
# File lib/stella/core_ext.rb, line 153
  def linkify!
    self.gsub!(/\b((https?:\/\/|ftps?:\/\/|mailto:|www\.|status\.)([A-Za-z0-9\-_=%&@\?\.\/]+(\/\s)?))\b/) {
      match = $1
      tail  = $3
      case match
      when /^(www|status)/     then  "<a href=\"http://#{match.strip}\">#{match}</a>"
      when /^mailto/  then  "<a href=\"#{match.strip}\">#{tail}</a>"
      else                  "<a href=\"#{match.strip}\">#{match}</a>"
      end
    }
    self
  end
plural (int=1)
[show source]
# File lib/stella/core_ext.rb, line 136
  def plural(int=1)
    int > 1 || int.zero? ? "#{self}s" : self
  end
shorten (len=50)
[show source]
# File lib/stella/core_ext.rb, line 139
  def shorten(len=50)
    return self if size <= len
    self[0..len] + "..."
  end
to_file (filename, mode, chmod=0744)
[show source]
# File lib/stella/core_ext.rb, line 143
  def to_file(filename, mode, chmod=0744)
    mode = (mode == :append) ? 'a' : 'w'
    f = File.open(filename,mode)
    f.puts self
    f.close
    raise "Provided chmod is not a Fixnum (#{chmod})" unless chmod.is_a?(Fixnum)
    File.chmod(chmod, filename)
  end