class String

Public Instance Methods

encode_fix(enc="UTF-8") click to toggle source
# File lib/stella/core_ext.rb, line 124
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() click to toggle source
# File lib/stella/core_ext.rb, line 114
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() click to toggle source
# File lib/stella/core_ext.rb, line 165
def linkify
   self.dup.linkify!
end
linkify!() click to toggle source

via: www.est1985.nl/design/2-design/96-linkify-urls-in-ruby-on-rails

# File lib/stella/core_ext.rb, line 152
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) click to toggle source
# File lib/stella/core_ext.rb, line 135
def plural(int=1)
  int > 1 || int.zero? ? "#{self}s" : self
end
shorten(len=50) click to toggle source
# File lib/stella/core_ext.rb, line 138
def shorten(len=50)
  return self if size <= len
  self[0..len] + "..."
end
to_file(filename, mode, chmod=0744) click to toggle source
# File lib/stella/core_ext.rb, line 142
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