[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Unlogical ?

Christer Nilsson

12/9/2005 7:41:00 AM

Hmm, why are these two assertions breaking?

def test_strange
assert_equal true, true && true
assert_equal false, true && false
assert_equal false, false && true
assert_equal false, false && false

assert_equal true, true and true
assert_equal false, false and true
assert_equal false, false and false
assert_equal false, true and false # breaks

assert_equal true, true || true
assert_equal true, true || false
assert_equal true, false || true
assert_equal false, false || false

assert_equal true, true or true
assert_equal true, true or false
assert_equal false, false or false
assert_equal true, false or true # breaks
end

Christer

--
Posted via http://www.ruby-....


6 Answers

Jeremy Kemper

12/9/2005 7:51:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Dec 8, 2005, at 11:40 PM, Christer Nilsson wrote:
> Hmm, why are these two assertions breaking?

assert_equal false, true or false
assert_equal(false, true) or false
assert_equal(false, (true or false))

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDmTdlAQHALep9HFYRAj6iAJ0eukjPlXB7XIobBcqQcZbQYtE7IgCfT/Ka
0gwx518M4IXNLwdG0wTvKqM=
=1CJC
-----END PGP SIGNATURE-----


Logan Capaldo

12/9/2005 7:52:00 AM

0


On Dec 9, 2005, at 2:40 AM, Christer Nilsson wrote:

> assert_equal true, false or true # breaks

or has relatively low precedence. I imagine ruby sees assert_equal
(true, false) or true.
Try throwing in some parens eg. assert_equal(true, false or true)


Benedikt Heinen

12/9/2005 7:54:00 AM

0

Christer Nilsson

12/9/2005 8:51:00 AM

0

ruby wrote:

> assert_equal (false, true && false)
> assert_equal (false, true) and false

Pickaxe does not list "," in the operator precedence table.

<snip from pickaxe>
&&
||
.. ...
? :
= etc
<<= etc
defined?
not
or and
</snip from pickaxe>

The only operator relating to "," here is "=".
Maybe it has to do with parallell assignment, as the comma is used
there.

a, b = false, true

My workaround will be to use "&&"

Christer


--
Posted via http://www.ruby-....


daz

12/9/2005 9:02:00 AM

0


Christer Nilsson wrote:
>
> Maybe it has to do with parallell assignment, as the comma is used
> there.
>

I don't think it's that.

>
> My workaround will be to use "&&"
>

Parens will work around either.


&&, || "give" you parenthesised left and right terms -
and, or don't.

a && b equates with: (a and b)

r = true and false
equates with:
((r = true) and false)

Test:
r2 = ((r = true) and false)

p [r, r2] #=> [true, false]


The following won't make anything clearer but it
may help you to understand why it's not clear ;)

r = true and false ; p r #=> true
r = true and (false) ; p r #=> true
r = (true ) and false ; p r #=> true
r = true && false ; p r #=> false
r = ( true and false ) ; p r #=> false
r = ( true and (false)) ; p r #=> false
r = ((true ) and false ) ; p r #=> false
puts
r = false or true ; p r #=> false
r = false or (true ) ; p r #=> false
r = (false) or true ; p r #=> false
r = false || true ; p r #=> true
r = ( false or true ) ; p r #=> true
r = ( false or (true )) ; p r #=> true
r = ((false) or true ) ; p r #=> true


daz



Gary Wright

12/10/2005 3:47:00 AM

0


On Dec 9, 2005, at 3:51 AM, Christer Nilsson wrote:
> ruby wrote:
>> assert_equal (false, true && false)
>> assert_equal (false, true) and false
>
> Pickaxe does not list "," in the operator precedence table.

Because ',' is not an operator. It is an argument separator.

>> assert_equal (false, true) and false

is the same as:

assert_equal(false,true) and false

I think the space between the method name and the opening
paren of the argument list is confusing you.

Gary Wright