Module: MPatch::Include::String

Defined in:
lib/mpatch/string.rb

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter = :upper) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mpatch/string.rb', line 36

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)


67
68
69
# File 'lib/mpatch/string.rb', line 67

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

#check(obj) ⇒ Object

return true or false



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mpatch/string.rb', line 83

def check obj

  case true

    when obj.class <= Regexp
      return ((self =~ obj).nil? ? false : true)

    when obj.class <= String
      return self.include?(obj)

    when obj.class <= Array
      obj.each do |element|
        if self.check(element) == true
          return true
        end
      end
      return false

    when obj.class <= Hash
      raise ArgumentError, "not implemented object for #{__method__} method"

    else
      return self.include?(obj.to_s)


  end

end

#dasherizeObject



46
47
48
# File 'lib/mpatch/string.rb', line 46

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

#demodulizeObject



52
53
54
# File 'lib/mpatch/string.rb', line 52

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



73
74
75
76
77
78
79
80
# File 'lib/mpatch/string.rb', line 73

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



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

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

#underscoreObject



58
59
60
61
62
63
64
# File 'lib/mpatch/string.rb', line 58

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