module MPatch::Include::String

Public Instance Methods

camelize(first_letter = :upper) click to toggle source

Standard in rails. See official documentation

api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html
# 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?() click to toggle source

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

# File lib/mpatch/string.rb, line 67
def capitalized?
  self.match(/^[[:upper:]]/) ? true : false
end
check(obj) click to toggle source

return true or false

# 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
dasherize() click to toggle source

Standard in rails. See official documentation

api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html
# File lib/mpatch/string.rb, line 46
def dasherize
  gsub(/_/, '-')
end
demodulize() click to toggle source

Standard in rails. See official documentation

api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html
# File lib/mpatch/string.rb, line 52
def demodulize
  gsub(/^.*::/, '')
end
frequency(str) click to toggle source

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

# 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) click to toggle source

Find string in othere string

# 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
underscore() click to toggle source

Standard in rails. See official documentation

api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html
# 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