1

I'm looking for a method comparable to javascript's Array.prototype.every method but in ruby. In javascript, it iterates over the array and returns true if every element causes the callback to return true. One of the nice things about it is it doesn't bother iterating over the whole array if one of the elements fails the test. Instead it short-circuits and returns false.

function isBigEnough(element) {
  return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough);   //=> false
[12, 54, 18, 130, 44].every(isBigEnough); //=> true

I know I could get a similar effect with lower level iterators like the while loop.

def isBigEnough(arr)
  i = 0
  result = true
  while i < arr.length
    if arr[i] >= 10
      i++
    else
      i = arr.length
      result = false
    end
  end
  return result
end
is_big_enough([12, 5, 8, 130, 44]) #=> false
is_big_enough([12, 54, 18, 130, 44]) #=> true

But I figured ruby would have something for this. Anyone know how to get this same effect?

4
  • Try the all? method. Commented Jun 24, 2015 at 0:18
  • 4
    if you downvote, please leave a comment explaining why Commented Jun 24, 2015 at 0:34
  • I did not downvote, but it shouldn't be that hard to figure out that someone might choose to downvote a question which could be answered by simply browsing the documentation or doing a quick Google search. Commented Jul 3, 2015 at 12:20
  • 1
    I recently forgot the method again and googled for the answer. I think it's telling that this post is not just the top result, it's the only result on page 1 with the correct answer. The reason stackoverflow exists is specifically to make it so people don't have to pour through dense documentation, wordy blogs, and massive forum threads. If you agree, please upvote this question. Commented Jan 3, 2017 at 20:42

2 Answers 2

5

I would use all?.

my_array.all? { |element| element >= 10 }

You pass in a block of code which is functionally equivalent to passing your function in JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

1
is_big_enough = -> element { element >= 10 }
[12, 5, 8, 130, 44].all?(&is_big_enough)   # => false
[12, 54, 18, 130, 44].all?(&is_big_enough) # => true

Enumerable#all? [{ |obj| block } ]true or false

Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } which will cause all? to return true when none of the collection members are false or nil.

%w[ant bear cat].all? { |word| word.length >= 3 } #=> true
%w[ant bear cat].all? { |word| word.length >= 4 } #=> false
[nil, true, 99].all?                              #=> false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.