Unless statement... why oh why?

(Patrick Fernie) #1

When you check thr directly, since thr is not nil (it is still a
Thread object, just dead), those parts of the test evaluate "true".
So, in:
"HERE" if not thr or not thr.alive?
the "not thr" evaluate false (since thr is an object, and therefore
not nil), but the "not thr.alive?" evaluates true (not false), so
"HERE" is printed since one of the tests is true

in:
"HERE" unless thr or the.alive?
"thr" is not nil, so it's true, so we short circuit and don't print,
since it's an unless.

in:
"HERE" unless the and the.alive?
"thr" is not nil, so it's true, but thr.alive? is false, so the "and"
overall evaluates to false, so "HERE" is printed since it's an unless.

Hope that makes sense.
-Patrick

···

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) #2

Sorry, to be a little more clear, the following 3 lines are all
equivalent (notice the parens in line 2):

if not thr or not thr.alive?
unless (thr or thr.alive?)
unless thr and thr.alive?

It's easiest for me to think of "unless" == "if not". What's funny is
that "if not" is no longer than "unless", so the "unless" keyword
doesn't buy us much.

···

On 8/18/05, Patrick Fernie <patrick.fernie@gmail.com> wrote:

When you check thr directly, since thr is not nil (it is still a
Thread object, just dead), those parts of the test evaluate "true".
So, in:
"HERE" if not thr or not thr.alive?
the "not thr" evaluate false (since thr is an object, and therefore
not nil), but the "not thr.alive?" evaluates true (not false), so
"HERE" is printed since one of the tests is true

in:
"HERE" unless thr or the.alive?
"thr" is not nil, so it's true, so we short circuit and don't print,
since it's an unless.

in:
"HERE" unless the and the.alive?
"thr" is not nil, so it's true, but thr.alive? is false, so the "and"
overall evaluates to false, so "HERE" is printed since it's an unless.

Hope that makes sense.
-Patrick

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

(Zach Dennis) #3

Patrick Fernie wrote:

When you check thr directly, since thr is not nil (it is still a
Thread object, just dead), those parts of the test evaluate "true".
So, in:
"HERE" if not thr or not thr.alive? the "not thr" evaluate false (since thr is an object, and therefore
not nil), but the "not thr.alive?" evaluates true (not false), so
"HERE" is printed since one of the tests is true

in:
"HERE" unless thr or the.alive?
"thr" is not nil, so it's true, so we short circuit and don't print,
since it's an unless.

in:
"HERE" unless the and the.alive?
"thr" is not nil, so it's true, but thr.alive? is false, so the "and"
overall evaluates to false, so "HERE" is printed since it's an unless.

Hope that makes sense.

Yes this lets my brain see things in a very simplistic fashion. Thank you for taking the time to respond!

Zach

(Brian Schröder) #4

I am quite shure that the above is not correct.

if (not thr or not thr.alive?)
is equal to
if not (thr or thr.alive?)
and
unless (thr and thr.alive?)
and
unless thr and thr.alive?

Proof:

irb(main):008:0> [[false, false], [false, true], [true, false], [true,
true]].map do | a, b | [("No Parens" unless a or b), ("Parens" unless
(a or b))] end
=> [["No Parens", "Parens"], [nil, nil], [nil, nil], [nil, nil]]

regards,

Brian

···

On 18/08/05, Brock Weaver <brockweaver@gmail.com> wrote:

Sorry, to be a little more clear, the following 3 lines are all
equivalent (notice the parens in line 2):

if not thr or not thr.alive?
unless (thr or thr.alive?)
unless thr and thr.alive?

It's easiest for me to think of "unless" == "if not". What's funny is
that "if not" is no longer than "unless", so the "unless" keyword
doesn't buy us much.

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

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

(David A. Black) #5

Hi --

···

On Fri, 19 Aug 2005, Brock Weaver wrote:

Sorry, to be a little more clear, the following 3 lines are all
equivalent (notice the parens in line 2):

if not thr or not thr.alive?
unless (thr or thr.alive?)
unless thr and thr.alive?

It's easiest for me to think of "unless" == "if not". What's funny is
that "if not" is no longer than "unless", so the "unless" keyword
doesn't buy us much.

I don't think its goal is to be shorter (apparently :slight_smile: but rather to
sound good.

David

--
David A. Black
dblack@wobblini.net

(Brock Weaver) #6

Thank you for correcting me, Brian. I got a little post happy I guess. :slight_smile:

···

On 8/18/05, Brian Schröder <ruby.brian@gmail.com> wrote:

On 18/08/05, Brock Weaver <brockweaver@gmail.com> wrote:
> Sorry, to be a little more clear, the following 3 lines are all
> equivalent (notice the parens in line 2):
>
> if not thr or not thr.alive?
> unless (thr or thr.alive?)
> unless thr and thr.alive?
>
> It's easiest for me to think of "unless" == "if not". What's funny is
> that "if not" is no longer than "unless", so the "unless" keyword
> doesn't buy us much.
>

I am quite shure that the above is not correct.

if (not thr or not thr.alive?)
is equal to
if not (thr or thr.alive?)
and
unless (thr and thr.alive?)
and
unless thr and thr.alive?

Proof:

irb(main):008:0> [[false, false], [false, true], [true, false], [true,
true]].map do | a, b | [("No Parens" unless a or b), ("Parens" unless
(a or b))] end
=> [["No Parens", "Parens"], [nil, nil], [nil, nil], [nil, nil]]

regards,

Brian

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

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

--
Brock Weaver
[OBC]Technique