I am trying to compare two arrays to ensure that the corresponding values of one is always greater than the other.
a = [2, 3, 4]
b = [1, 2, 3]
# a[0] > b[0] ... a[x] > b[x]
At this point I am thinking of using inject
with an index and return if the comparison fails, like:
b.each_with_index.inject(true) do |cmp, (element,index)|
if element > a[index] do
cmp = false
return
end
end
Is there a better way of doing this? Kinda feeling like Ruby or Rails might already have something like this built-in and I missed it.
This is what I would do:
a.zip(b).all? { |a, b| a > b }
Note though that zip
will truncate in case the two arrays are not of the same size.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments