Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/stella/core_ext.rb,
lib/stella/core_ext.rb
Overview
Assumes Time::Units and Numeric mixins are available.
Instance Method Summary collapse
- #encode_fix(enc = "UTF-8") ⇒ Object
- #in_seconds ⇒ Object
- #linkify ⇒ Object
- #linkify! ⇒ Object
- #plural(int = 1) ⇒ Object
- #shorten(len = 50) ⇒ Object
- #to_file(filename, mode, chmod = 0744) ⇒ Object
Instance Method Details
#encode_fix(enc = "UTF-8") ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# 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 ⇒ Object
115 116 117 118 119 120 |
# 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 ⇒ Object
166 167 168 |
# File 'lib/stella/core_ext.rb', line 166 def linkify self.dup.linkify! end |
#linkify! ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# 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) ⇒ Object
136 137 138 |
# File 'lib/stella/core_ext.rb', line 136 def plural(int=1) int > 1 || int.zero? ? "#{self}s" : self end |
#shorten(len = 50) ⇒ Object
139 140 141 142 |
# 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) ⇒ Object
143 144 145 146 147 148 149 150 |
# 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 |