Beginner question on ruby operator precedence

In app/models/article.rb:

    def price_changed?
      changed.detect { |attr| attr == 'price' || 'tax' || 'deposit' || 'unit_quantity' } ? true : false
    end

Is it safe to do this? I understand that || has another precedence than or, but what happens here? My tests in a console were not successful.

Hey Julius,

if i get it right u can do that:

In app/models/article.rb:

def price_changed?

!(a.changed & %w(price tax deposit unit_quantity)).empty?

The & returns a new array with elements in both arrays. if it’s empty the statement returns false (see ! at the beginning).

changed.detect { |attr| attr == ‘price’ || ‘tax’ || ‘deposit’ || ‘unit_quantity’ } ? true : false
end

Is it safe to do this? I understand that || has another precedence than or, but what happens here? My tests in a console were not successful.

What do u mean? if i run your code in the console it has the same effect as my line. seems to work!

here is a stackoverflow with more options:
http://stackoverflow.com/questions/8026300/check-for-multiple-items-in-array-using-include-ruby-beginner

Hey, thank you for you answer.

What do u mean? if i run your code in the console it has the same effect as my line. seems to work!

I should have included my test to explain what I am interested in specifically. However, now I am quite sure that it is a bug.

Here is the version implemented
1.9.3p484 :001 > [‘name’, ‘supplier_id’].detect {|attr| attr == ‘price’ || ‘tax’} ? true : false
=> true

Here are different other versions (a slight modification, your intersection version and a stackoverflow suggestion)
1.9.3p484 :002 > [‘name’, ‘supplier_id’].detect {|attr| attr == ‘price’ || attr == ‘tax’} ? true : false
=> false
1.9.3p484 :003 > !([‘name’, ‘supplier_id’] & %w(price tax)).empty?
=> false
1.9.3p484 :004 > [‘name’, ‘supplier_id’].any? {|attr| [‘price’, ‘tax’].include? attr}
=> false

Am I right that the first version is buggy? I hope we both understand the purpose of the line correctly.

add braces and it works.

[‘name’, ‘supplier_id’].detect {|attr| attr == (‘price’ || 'tax‘)} ? true : false