unless statement... why oh why?

Z

Zach Dennis

thr = Thread.new{}
# thread dies

"HERE" if not thr or not thr.alive? # this works!!
"HERE" unless thr or thr.alive? # doesn't work!! WHY?
"HERE" unless thr and thr.alive? # works, but why?

I dont see why the unless/and works when the unless/or should be the one
short-circuting no?

Thanks,

Zach
 
Z

Zach Dennis

Zach said:
thr = Thread.new{}
# thread dies

"HERE" if not thr or not thr.alive? # this works!!
"HERE" unless thr or thr.alive? # doesn't work!! WHY?
"HERE" unless thr and thr.alive? # works, but why?

I dont see why the unless/and works when the unless/or should be the one
short-circuting no?

ignore the "short-circuting" comment in reference to the or statement.

Zach
 
B

Brock Weaver

DeMorgan's theorem and operator precedence in ruby.

DeMorgan's theorem in a nutshell:

(Not A) and (Not B) =3D=3D Not (A or B)

Your code is

(Not A) and (Not B) =3D=3D (Not A) or (B)

See the difference?

thr =3D Thread.new{}
# thread dies
=20
"HERE" if not thr or not thr.alive? # this works!!
"HERE" unless thr or thr.alive? # doesn't work!! WHY?
"HERE" unless thr and thr.alive? # works, but why?
=20
I dont see why the unless/and works when the unless/or should be the one
short-circuting no?
=20
Thanks,
=20
Zach
=20
=20


--=20
Brock Weaver
[OBC]Technique
 
B

Brian Schröder

thr =3D Thread.new{}
# thread dies
=20
"HERE" if not thr or not thr.alive? # this works!!

(not a) or (not b) <=3D> not (a and b)
<=3D/=3D>=20
not (a or b) said:
"HERE" unless thr or thr.alive? # doesn't work!! WHY?
"HERE" unless thr and thr.alive? # works, but why?
=20
I dont see why the unless/and works when the unless/or should be the one
short-circuting no?
=20
Thanks,
=20
Zach
=20

brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
Z

Zach Dennis

Brock said:
DeMorgan's theorem and operator precedence in ruby.

DeMorgan's theorem in a nutshell:

(Not A) and (Not B) == Not (A or B)

Your code is

(Not A) and (Not B) == (Not A) or (B)

See the difference?

Yep, thank you for the quick response!

Zach
 
P

Phrogz

Or, put another way:
When you are performing boolean negation on an expression:
* Change all ANDs to ORs, and vice-versa
* Throw a NOT in front of every value
* (And then, to be clean, change "NOT NOT a" to just "a")

Your original was "(NOT a) OR (NOT b)"
Negating, you get "NOT( (NOT a) OR (NOT b) ) => NOT(NOT a) AND NOT(NOT
b) => a AND b"

If you're still not convinced, sound it out logically:

"DON'T play NFL football IF you're NOT alive OR NOT male"

Is this the opposite?
"DO play NFL football IF you're alive OR male"
No, because that means that alive women may play.

How about this?
"DO play NFL football IF you're alive AND male"
Ah, that's the sexist sport we know and love!
 
H

Hal Fulton

Zach said:
thr = Thread.new{}
# thread dies

"HERE" if not thr or not thr.alive? # this works!!
"HERE" unless thr or thr.alive? # doesn't work!! WHY?
"HERE" unless thr and thr.alive? # works, but why?

I dont see why the unless/and works when the unless/or should be the one
short-circuting no?

I don't think it's a matter of short-circuiting, but
a matter of DeMorgan's Theorem...


Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top