Class: String

Inherits:
Object show all
Defined in:
lib/olelo/html_safe.rb,
lib/olelo/extensions.rb

Direct Known Subclasses

HtmlString

Defined Under Namespace

Classes: HtmlString

Instance Method Summary collapse

Instance Method Details

#/(path) ⇒ String

Concatenate path components with /

Calls #cleanpath on result.

Parameters:

Returns:

  • (String)

    this string concatenated with path



225
226
227
# File 'lib/olelo/extensions.rb', line 225

def /(path)
  "#{self}/#{path}".cleanpath
end

#cleanpathString

Clean up path (replaces ‘..’, ‘.’ etc.)

Returns:



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/olelo/extensions.rb', line 203

def cleanpath
  names = []
  split('/').each do |name|
    case name
    when '..'
      names.pop
    when '.'
    when ''
    else
      names.push name
    end
  end
  names.join('/')
end

#ends_with?(s) ⇒ Boolean

Check if string ends with s

Parameters:

Returns:

  • (Boolean)


195
196
197
# File 'lib/olelo/extensions.rb', line 195

def ends_with?(s)
  rindex(s) == size - s.size
end

#html_safeObject



22
23
24
# File 'lib/olelo/html_safe.rb', line 22

def html_safe
  HtmlString.new(self)
end

#starts_with?(s) ⇒ Boolean

Check if string starts with s

Parameters:

Returns:

  • (Boolean)


186
187
188
# File 'lib/olelo/extensions.rb', line 186

def starts_with?(s)
  index(s) == 0
end

#try_encoding(enc) ⇒ Object

Try to force encoding

Force encoding of string and revert to original encoding if string has no valid encoding

Parameters:

  • enc (Encoding, String)

    New encoding

Returns:

  • self



172
173
174
175
176
177
178
179
# File 'lib/olelo/extensions.rb', line 172

def try_encoding(enc)
  old_enc = encoding
  if old_enc != enc
    force_encoding(enc)
    force_encoding(old_enc) if !valid_encoding?
  end
  self
end