Class: Array

Inherits:
Object show all
Defined in:
lib/sitefuel/extensions/ArrayComparisons.rb

Overview

File

ArrayComparisons.rb

Author

wkm

Copyright

2009, Zanoccio LLC.

License

GPL version 2.0 (see LICENSE.rb)

Adds methods to Array for comparing with other arrays

Instance Method Summary collapse

Instance Method Details

#ends_with?(other) ⇒ Boolean

gives true if self ends with the given array

[1, 2, 3].ends_with? [2, 3]  # ==> true
[1, 2, 3].ends_with? []  # ==> true
[1, 2, 3].ends_with? [1, 2, 3]  # ==> true
[1, 2, 3].ends_with? [1, 2]  # ==> false

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sitefuel/extensions/ArrayComparisons.rb', line 18

def ends_with?(other)
  # if the supposed 'ending' is longer, it can't be an ending
  if length < other.length
    return false
  end

  index = -1
  while -index <= other.length and -index <= length
    if other[index] != self[index]
      return false
    end
    index -= 1
  end
  
  return true
end