Module: BerkeleyLibrary::Util::Strings
Constant Summary collapse
- ASCII_0 =
'0'.ord
- ASCII_9 =
'9'.ord
Instance Method Summary collapse
- #ascii_numeric?(s) ⇒ Boolean
-
#diff_index(s1, s2) ⇒ Integer?
Locates the point at which two strings differ.
Instance Method Details
#ascii_numeric?(s) ⇒ 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
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 |