Module: MPatch::String

Defined in:
lib/mpatch/string.rb

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter = :upper) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/mpatch/string.rb', line 33

def camelize(first_letter = :upper)
  if first_letter == :upper
    gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    self[0..0].downcase + camelize[1..-1]
  end
end

#capitalized? ⇒ Boolean

Check that instance of String is start with an upper case or not

Returns:

  • (Boolean)


64
65
66
# File 'lib/mpatch/string.rb', line 64

def capitalized?
  self.match(/^[[:upper:]]/) ? true : false
end

#dasherize ⇒ Object



43
44
45
# File 'lib/mpatch/string.rb', line 43

def dasherize
  gsub(/_/, '-')
end

#demodulize ⇒ Object



49
50
51
# File 'lib/mpatch/string.rb', line 49

def demodulize
  gsub(/^.*::/, '')
end

#frequency(str) ⇒ Object

return the number how often the str is with in the self by default with \b regex border



70
71
72
73
74
75
76
77
# File 'lib/mpatch/string.rb', line 70

def frequency(str)
  begin
    if str.class == String
      str= '\b'+str+'\b'
    end
  end
  self.scan(/#{str}/).count
end

#positions(oth_string) ⇒ Object

Find string in othere string



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mpatch/string.rb', line 5

def positions(oth_string)

  special_chrs=%w[# _ & < > @ $ . , -]+[*(0..9)]+[*("A".."Z")]+[*("a".."z")]
  loop do
    if oth_string.include? special_chrs[0]
      special_chrs.shift
    else
      break
    end
  end

  string=self
  return_array = []
  loop do
    break if string.index(oth_string).nil?
    range_value= ((string.index(oth_string))..(string.index(oth_string)+oth_string.length-1))
    return_array.push range_value
    [*range_value].each do |one_index|
      string[one_index]= special_chrs[0]
    end
  end

  # return value
  return return_array
end

#underscore ⇒ Object



55
56
57
58
59
60
61
# File 'lib/mpatch/string.rb', line 55

def underscore
  gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
end