Module: SameAs

Included in:
Array
Defined in:
lib/same_as/same_as.rb,
lib/same_as/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#same_as?(other_ary) ⇒ Boolean

Returns true if each element in self is equal to corresponding element in the other array, with the comparison method given in the block. Returns false otherwise. With no block, compares the elements using “==.”



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/same_as/same_as.rb', line 7

def same_as?(other_ary) # :yields:
  is_same_as = true
  
  if other_ary != nil && self.size == other_ary.size
     self.each_with_index do |x, i|
       if block_given?
         is_same_as &&= yield self[i], other_ary[i]
       else
         is_same_as &&= (self[i] == other_ary[i])
       end
     end
  else
    is_same_as = false
  end
  
  return is_same_as
end

#same_prefix?(other_ary) ⇒ Boolean

Returns true if either of each corresponding strings in self and the other array includes the other from the first character. Returns false if otherwise.



28
29
30
# File 'lib/same_as/same_as.rb', line 28

def same_prefix?(other_ary)
  return self.same_as?(other_ary) {|a, b| a.start_with?(b) || b.start_with?(a)}
end