Beginner question on ruby operator precedence

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Beginner question on ruby operator precedence

JuliusR
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.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question on ruby operator precedence

fsmanuel
Administrator
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!



If you reply to this email, your message will be added to the discussion below:
http://foodsoft.51229.x6.nabble.com/Beginner-question-on-ruby-operator-precedence-tp318.html
To start a new topic under foodsoft-dev, email [hidden email]
To unsubscribe from foodsoft-dev, click here.
NAML


signature.asc (211 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question on ruby operator precedence

fsmanuel
Administrator
In reply to this post by JuliusR
here is a stackoverflow with more options:


Am 30.12.2013 um 14:29 schrieb JuliusR [via foodsoft] <[hidden email]>:

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.


If you reply to this email, your message will be added to the discussion below:
http://foodsoft.51229.x6.nabble.com/Beginner-question-on-ruby-operator-precedence-tp318.html
To start a new topic under foodsoft-dev, email [hidden email]
To unsubscribe from foodsoft-dev, click here.
NAML


signature.asc (211 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question on ruby operator precedence

JuliusR
In reply to this post by fsmanuel
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.
Reply | Threaded
Open this post in threaded view
|

Re: Beginner question on ruby operator precedence

fsmanuel
Administrator
add braces and it works.


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

Am 02.01.2014 um 13:51 schrieb JuliusR [via foodsoft] <[hidden email]>:

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.


If you reply to this email, your message will be added to the discussion below:
http://foodsoft.51229.x6.nabble.com/Beginner-question-on-ruby-operator-precedence-tp318p321.html
To start a new topic under foodsoft-dev, email [hidden email]
To unsubscribe from foodsoft-dev, click here.
NAML


signature.asc (211 bytes) Download Attachment