I more or less commonly use the following idiom (taken from recent
code):
dir = ARGV.shift || raise “Require directory argument”
Then I find that it fails:
/home/gavin/bin/ckcr:3: parse error
/home/gavin/bin/ckcr:3: warning: useless use of a literal in void context
This fixes it:
dir = ARGV.shift || raise(“Require directory argument”)
Can anyone explain why the parentheses are required? Not a critical
error or anything. I’ve just always wanted to know but been afraid to
ask.
Regards,
Gavin
dir = ARGV.shift || raise “Require directory argument”
Is probably getting parsed as:
dir = (ARGV.shift || raise) “Require directory argument”
When you type
dir = ARGV.shift || raise(“Require directory argument”)
Ruby parses it as
dir = ARGV.shift || (raise(“Require directory argument”))
Use ‘or’ instead of ||, it has lower precedence.
···
On Tue, Jan 28, 2003 at 09:55:59AM +0900, Gavin Sinclair wrote:
I more or less commonly use the following idiom (taken from recent
code):
dir = ARGV.shift || raise “Require directory argument”
Then I find that it fails:
/home/gavin/bin/ckcr:3: parse error
/home/gavin/bin/ckcr:3: warning: useless use of a literal in void context
This fixes it:
dir = ARGV.shift || raise(“Require directory argument”)
Can anyone explain why the parentheses are required? Not a critical
error or anything. I’ve just always wanted to know but been afraid to
ask.
Regards,
Gavin
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
dir = ARGV.shift || raise “Require directory argument”
is like saying
dir = (ARGV.shift || raise) “Require directory argument”
you could say
dir = ARGV.shift or raise “Require directory argument”
but that is like
(dir = ARGV.shift) or raise “Require directory argument”
because = binds more tightly that ‘or’
personally i like
dir = (ARGV.shift or raise “Require directory argument”)
because it is visually clear that dir will be ‘one of the things in the
parens’ and it allows poetry mode methods and plain text booleans.
-a
···
On Tue, 28 Jan 2003, Gavin Sinclair wrote:
I more or less commonly use the following idiom (taken from recent
code):
dir = ARGV.shift || raise “Require directory argument”
Then I find that it fails:
/home/gavin/bin/ckcr:3: parse error
/home/gavin/bin/ckcr:3: warning: useless use of a literal in void context
This fixes it:
dir = ARGV.shift || raise(“Require directory argument”)
Can anyone explain why the parentheses are required? Not a critical
error or anything. I’ve just always wanted to know but been afraid to
ask.
binds more tightly that (). so
–
====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
Hi –
dir = ARGV.shift || raise “Require directory argument”
Is probably getting parsed as:
dir = (ARGV.shift || raise) “Require directory argument”
When you type
dir = ARGV.shift || raise(“Require directory argument”)
Ruby parses it as
dir = ARGV.shift || (raise(“Require directory argument”))
In fact, I believe it’s even:
dir = (ARGV.shift || (raise(“Require directory argument”)))
meaning, dir is getting assigned to whether ARGV.shift works or not
(the return value of raise). This may not matter, since .shift ==
nil and raise returns nil, but still, it’s probably good to be aware
of.
Use ‘or’ instead of ||, it has lower precedence.
(And solves any weirdness of the above kind
David
···
On Tue, 28 Jan 2003, Daniel Carrera wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav