Unless statement... why oh why?

(Zach Dennis) #1

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

(Zach Dennis) #2

Zach Dennis wrote:

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

(Brock Weaver) #3

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?

···

On 8/18/05, Zach Dennis <zdennis@mktec.com> wrote:

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

--
Brock Weaver
[OBC]Technique

(Brian Schröder) #4

thr = Thread.new{}
# thread dies

"HERE" if not thr or not thr.alive? # this works!!

(not a) or (not b) <=> not (a and b)
  <=/=>
not (a or b) <=> (not a) and (not b)

"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

brian

···

On 18/08/05, Zach Dennis <zdennis@mktec.com> wrote:

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

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

(HAL 9000) #5

Zach Dennis wrote:

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

(Zach Dennis) #6

Brock Weaver wrote:

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

(Gavin Kistner) #7

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!