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. |
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 : falseWhat do u mean? if i run your code in the console it has the same effect as my line. seems to work!
|
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: |
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. |
Administrator
|
add braces and it works. Am 02.01.2014 um 13:51 schrieb JuliusR [via foodsoft] <[hidden email]>: Hey, thank you for you answer. |
Free forum by Nabble | Edit this page |