Tuesday, 15 March 2011

Ruby Boolean Operations -


In the train (i.e., ruby), I need either X and Y to be true or true: / P>

  (x & y) || A  

I suspect that I can write

  x & amp; Amp; Y || One  

But I do not know the superiority of Ruby works. I did some tests and I was still not sure (especially with an anomaly on line 3):

  true | True & amp; False # = & gt; True truth || True & amp; Amp; False # = & gt; True or true and false # = & gt; False # ah? True | False & amp; True # = & gt; True truth || Wrong & amp; Amp; True # = & gt; True or false and correct # = & gt;  ||  and  |  

Bonus points * (and and equivalent to - and or - I believe that English Versions are restricted to Ruby StyleGuide).

But really my question is how to write (x & y)? Without a bracket

* Bonus point is only imaginary because @sawa (in the comments below) is Sheldon Cooper secretly

either x and y are true or just a

Assume that I understand you correctly, I write it to ( X & amp; Y) || A , as you had suggested in your question, with brackets, clarifies how to behave in spite of priority, to others.

However, to answer your question, yes, you can leave the field in this case, as shown by the test case wrong & amp; Amp; Wrong || True # = & gt; True vs wrong & amp; Amp; (False || true) = & gt; False shows that & amp; Amp; Amp; And is the priority of at least || (This is actually a high priority, as true || false & false # = & gt; true )

The main difference between = and (and similarly, with booleans in Ruby and & amp; and & amp; ) is that | Boolean classes have a method, while > is a language-level boolean operator. This means that || supports short circuit evaluation, while | does not, in addition, | Both and and have priority over and and = ( & amp; are top priority ), As false & amp; Shown by amp; Wrong | True # = & gt; Incorrect and true | False & amp; False # = & gt; True .

Now you are performing bullion operations like, || and & amp; Amp; And almost always happens what you want. For the difference between

= and or (or similar), that question has already been asked twice They may want to see it and as they should answer your question in that respect. (The short answer is that or and and are statement level operators, while || and & amp; amp; Expression-level.)


No comments:

Post a Comment