Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/underglow/extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#ascii_only!Object

Sanitizes a string and leaves behind only ascii characters, and gets rid of non-ascii and does not change original encoding



99
100
101
102
103
# 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) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

#deurlizeObject



60
61
62
# File 'lib/underglow/extensions/string.rb', line 60

def deurlize
  gsub("-", "_")
end

#deurlize_to_symObject

deurlizes to symbol



65
66
67
# File 'lib/underglow/extensions/string.rb', line 65

def deurlize_to_sym
  deurlize.downcase.to_sym
end

#extract!(regexp, &block) ⇒ Object

Removes matched portion from string and returns matched data object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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_attributifyObject

make it suitable for html attributes



70
71
72
# File 'lib/underglow/extensions/string.rb', line 70

def html_attributify
  downcase.gsub(/[_\/\s]/, "-").gsub(/[^0-9a-z\-]+/, "")
end

#initial_capitalizeObject

only capitalize initial letter and leave the rest alone



49
50
51
52
53
54
# File 'lib/underglow/extensions/string.rb', line 49

def initial_capitalize
  str = self
  str[0] = str[0].chr.capitalize

  str
end

#numeric?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/underglow/extensions/string.rb', line 2

def numeric?
  true if Float(self) rescue false
end

#overlap(b) ⇒ Object

Concat another string and overlap it if it does



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
# 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

#urlizeObject



56
57
58
# File 'lib/underglow/extensions/string.rb', line 56

def urlize
  downcase.gsub("_", "-")
end