Another logical precedence question

J

-j b-

I am used to using the following shortcut:

value = ARGV.shift or usage

where usage is a method.
However, I recently ran into a situation where the following did not
work (as expected, anyway):

$user_type = ARGV.shift or usage
$user_map = ARGV.shift or DEFAULT_MAP

DEFAULT_MAP is just a string constant. When I tried to access $user_map
later I was surprised to find that it was nil when no argument was
provided. If I changed things to:

$user_type = ARGV.shift or usage
$user_map = (ARGV.shift or DEFAULT_MAP)

I got behavior I expected ($user_map was assigned DEFAULT_MAP).

Why the two types of behavior based (as far as I can tell) on the type
of the second parameter to 'or'?
 
T

ThoML

I was surprised to find that it was nil when no argument was
provided. If I changed things to:

$user_type = ARGV.shift or usage
$user_map = (ARGV.shift or DEFAULT_MAP)

Consider the following example:
irb(main):001:0> a = nil or 1
=> 1
irb(main):002:0> a
=> nil
irb(main):003:0> a = nil || 1
=> 1
irb(main):004:0> a
=> 1

So what you're doing is
(ut = ARGV.shift) or usage
but I'm pretty sure that what you want to do is
ut = ARGV.shift || usage
 
R

Rob Biedenharn

I am used to using the following shortcut:

value = ARGV.shift or usage

where usage is a method.
However, I recently ran into a situation where the following did not
work (as expected, anyway):

$user_type = ARGV.shift or usage
$user_map = ARGV.shift or DEFAULT_MAP

DEFAULT_MAP is just a string constant. When I tried to access
$user_map
later I was surprised to find that it was nil when no argument was
provided. If I changed things to:

$user_type = ARGV.shift or usage
$user_map = (ARGV.shift or DEFAULT_MAP)

I got behavior I expected ($user_map was assigned DEFAULT_MAP).

Why the two types of behavior based (as far as I can tell) on the type
of the second parameter to 'or'?


If you choose to use 'or' rather than '||', you are not just getting
different precedence ('or' is lower than '||'), but there are a bunch
of operators that fall in the middle.

In particular, the assignment operators are higher precedence than
'or' and lower precedence than '||'.

So you really have:

(value = ARGV.shift) or (usage)
($user_type = ARGV.shift) or (usage)
($user_map = ARGV.shift) or (DEFAULT_MAP)

When you change the operator to '||', the precedence becomes the
equivalent of:

value = (ARGV.shift || usage)
$user_type = (ARGV.shift || usage)
$user_map = (ARGV.shift || DEFAULT_MAP)

Generally, the use of the ultra-low precedence 'and', 'or', 'not'
operators is best reserved for places where the precedence does what
you expect. For me, this typically means thinking of these as
combining "statements" (which are often either assignment or flow
control) rather than expressions (for which I tend to always use '&&',
'||', '!' although my background with C and Perl make this sub-
conscious in most cases).

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
A

ara.t.howard

Why the two types of behavior based (as far as I can tell) on the type
of the second parameter to 'or'?

it's always the *same* behavior, namely

(something = this) or that

so when 'that' happens to be a method call which prints out usage and
exits you get the behavior you want be accident, when you do

user_map = ARGV.shift or DEFAULT_MAP

you simply do this

cfp:~ > irb=> nil

in otherwords

(user_map = nil) or DEFAULT_MAP

so the 'or' bit is essentially a noop you execute after assigning nil
to the user_map

regards.

a @ http://codeforpeople.com/
 
J

-j b-

ThoML said:
So what you're doing is
(ut = ARGV.shift) or usage
but I'm pretty sure that what you want to do is
ut = ARGV.shift || usage

I see.
Invoking the call and invoking the string are NOT the same thing.
*blush*
Thanks for the help.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top