class String
Public Instance Methods
ascii_only!()
click to toggle source
Sanitizes a string and leaves behind only ascii characters, and gets rid of non-ascii and does not change original encoding
# File lib/underglow/extensions/string.rb, line 99 def ascii_only! original_encoding = self.encoding encode!("US-ASCII", invalid: :replace, undef: :replace, replace: "") encode!(original_encoding.name) end
coerce(type = nil)
click to toggle source
Coerces to Float or Fixnum otherwise String
If no type is given, it will determine the type to coerce to If type is given (the standard type symbols like :integer, :string, etc), it will coerce to that type
# File lib/underglow/extensions/string.rb, line 15 def coerce(type = nil) if type.nil? if numeric? self.strip.match(/^\d+$/) ? self.to_i : self.to_f elsif self.match(/true/i) true elsif self.match(/false/i) false else self end else type = type.to_sym case type when :string self when :integer self.to_i when :float self.to_f when :boolean # only true matches to true, else false for everything if self.match(/true/i) true else false end else # unknown type, just return string self end end end
deurlize()
click to toggle source
# File lib/underglow/extensions/string.rb, line 60 def deurlize gsub("-", "_") end
deurlize_to_sym()
click to toggle source
deurlizes to symbol
# File lib/underglow/extensions/string.rb, line 65 def deurlize_to_sym deurlize.downcase.to_sym end
extract!(regexp, &block)
click to toggle source
Removes matched portion from string and returns matched data object
# File lib/underglow/extensions/string.rb, line 106 def extract!(regexp, &block) raise ArgumentError, "Must pass in a Regexp object!" unless regexp.is_a? Regexp match = regexp.match(self) if match sub!(regexp, "") if block_given? block.call(match) else return match end end nil end
html_attributify()
click to toggle source
make it suitable for html attributes
# File lib/underglow/extensions/string.rb, line 70 def html_attributify downcase.gsub(/[_\/\s]/, "-").gsub(/[^0-9a-z\-]+/, "") end
initial_capitalize()
click to toggle source
only capitalize initial letter and leave the rest alone
# File lib/underglow/extensions/string.rb, line 49 def initial_capitalize str = self str[0] = str[0].chr.capitalize str end
numeric?()
click to toggle source
# File lib/underglow/extensions/string.rb, line 2 def numeric? true if Float(self) rescue false end
overlap(b)
click to toggle source
Concat another string and overlap it if it does
# File lib/underglow/extensions/string.rb, line 75 def overlap(b) a = self a_len = self.length b_len = b.length n = nil (0..a_len-1).each do |i| j = i k = 0 while j < a_len and k < b_len and a[j] == b[k] j += 1 k += 1 end n = k and break if j == a_len end n ||= 0 a + b[n..b_len-1] end
url?()
click to toggle source
# File lib/underglow/extensions/string.rb, line 6 def url? return true if %r{(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))}.match(self) false end
urlize()
click to toggle source
# File lib/underglow/extensions/string.rb, line 56 def urlize downcase.gsub("_", "-") end