I'm using Ruby 2.4. How do I scan every element of an array for a condition except one index in the array? I tried this
arr.except(2).any? {|str| str.eql?("b")}
But got the following error:
NoMethodError: undefined method `except' for ["a", "b", "c"]:Array
but evidently what I read online about "except" is greatly exaggerated.
arr.reject.with_index { |_el, index| index == 2 }.any? { |str| str.eql?("b") }
Explanation:
arr = [0, 1, 2, 3, 4, 5]
arr.reject.with_index { |_el, index| index == 2 }
#=> [0, 1, 3, 4, 5]
Shortening what you are doing:
arr.reject.with_index { |_el, index| index == 2 }.grep(/b/).any?
#=> true
Following the @Cary's comment, another option would be:
arr.each_with_index.any? { |str, i| i != 2 && str.eql?("b") }