Class: String

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

Instance Method Summary collapse

Instance Method Details

#compare_to(another_string) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/string.rb', line 4

def compare_to(another_string)
  value = self.bytes
  other = another_string.bytes
  len1 = value.size
  len2 = other.size
  lim = [len1, len2].min

  0.upto(lim - 1) do |k|
    if value[k] != other[k]
      return get_char(value, k) - get_char(other, k)
    end
  end
  len1 - len2
end

#get_char(val, index) ⇒ Object



19
20
21
# File 'lib/string.rb', line 19

def get_char(val, index)
  val[index] & 0xff
end

#underscoreObject



23
24
25
26
27
28
29
# File 'lib/string.rb', line 23

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