Class: String

Inherits:
Object show all
Defined in:
lib/hobo_support.rb,
lib/hobo_support/string.rb

Instance Method Summary collapse

Instance Method Details

#remove(string_or_rx) ⇒ Object



3
4
5
# File 'lib/hobo_support/string.rb', line 3

def remove(string_or_rx)
  sub(string_or_rx, '')
end

#remove!(string_or_rx) ⇒ Object



7
8
9
# File 'lib/hobo_support/string.rb', line 7

def remove!(string_or_rx)
  sub!(string_or_rx, '')
end

#remove_all(string_or_rx) ⇒ Object



11
12
13
# File 'lib/hobo_support/string.rb', line 11

def remove_all(string_or_rx)
  gsub(string_or_rx, '')
end

#remove_all!(string_or_rx) ⇒ Object



15
16
17
# File 'lib/hobo_support/string.rb', line 15

def remove_all!(string_or_rx)
  gsub!(string_or_rx, '')
end

#safe_constantizeObject

Return the constant that this string refers to, or nil if ActiveSupport cannot load such a constant. This is much safer than ‘rescue NameError`, as that will mask genuine NameErrors that have been made in the code being loaded (#safe_constantize will not)



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hobo_support.rb', line 28

def safe_constantize
  Object.class_eval self
rescue NameError => e
  # Unfortunately we have to rely on the error message to figure out which constant was missing.
  # NameError has a #name method but it is always nil
  if e.message !~ /\b#{self}$/
    # oops - some other name error
    raise
  else
    nil
  end
end