Module: BerkeleyLibrary::Util::Strings

Extended by:
Strings
Included in:
Strings
Defined in:
lib/berkeley_library/util/strings.rb

Constant Summary collapse

ASCII_0 =
'0'.ord
ASCII_9 =
'9'.ord

Instance Method Summary collapse

Instance Method Details

#ascii_numeric?(s) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
# File 'lib/berkeley_library/util/strings.rb', line 8

def ascii_numeric?(s)
  s.chars.all? do |c|
    ord = c.ord
    ord.between?(ASCII_0, ASCII_9)
  end
end

#diff_index(s1, s2) ⇒ Integer?

Locates the point at which two strings differ

Returns:

  • (Integer, nil)

    the index of the first character in either string that differs from the other, or nil if the strings are identical, or are not strings



20
21
22
23
24
25
26
27
28
# File 'lib/berkeley_library/util/strings.rb', line 20

def diff_index(s1, s2)
  return unless string_like?(s1, s2)

  shorter, longer = s1.size > s2.size ? [s2, s1] : [s1, s2]
  shorter.chars.each_with_index do |c, i|
    return i if c != longer[i]
  end
  shorter.length if shorter.length < longer.length # otherwise they're equal
end