I want to make the following code more simply.
.========================================
if error_flag then
print usage_msg
exit 1
end
.========================================
I wrote it like the following, but usage_msg is not printed.
.========================================
print usage_msg, exit 1 if error_flag
.========================================
That should be “and” rather than “or” for both to be done if error_flag
??
pigeon% cat b.rb
#!/usr/bin/ruby
error_message = “error\n”
error_flag = true
print error_message and exit 1 if error_flag
puts “after”
print error_message or exit 1 if error_flag
puts “after”
pigeon%
pigeon% b.rb
error
after
error
pigeon%
Hmmm. I always get tangled with these. Bracketing things doesn’t help
here (I tried it). I’d have expected the or to “short circuit” after
the print succeeded… Oh, but print returns Nil, is that it? Hmmm.
I’m thinking in Shell I suppose! Or Icon…
These words are unhelpful, anyway, in so far as colloquial use of them
can conflict with the boolean use: English sometimes treats And as Set
Union, and treats Or as Intersect Not. “Find documents containing
‘cars’ or ‘buses’”… [cf Alan Cooper’s “About Face”]
I would “simplify” the original code just by leaving the word “then”
out. The block structure simplifies re-structuring the code in the
future. Also, error flags are probably best dealt with using raise
and begin…rescue…end blocks, IMHO.
Hmmm. I always get tangled with these. Bracketing things doesn’t help
here (I tried it). I’d have expected the or to “short circuit” after
the print succeeded… Oh, but print returns Nil, is that it? Hmmm.
I’m thinking in Shell I suppose! Or Icon…
You could make a virtue of necessity and decide that lines like this
should start with ! to draw attention to themselves:
error_msg = “error”
error = true
! puts error_msg and exit 1 if error
David
···
On Thu, 18 Jul 2002, Hugh Sasse Staff Elec Eng wrote:
if you could accept these unconventional application
– Gotoken
···
At Thu, 18 Jul 2002 23:26:38 +0900, Hugh Sasse Staff Elec Eng wrote:
print error_message or exit 1 if error_flag
Hmmm. I always get tangled with these. Bracketing things doesn’t help
here (I tried it). I’d have expected the or to “short circuit” after
the print succeeded… Oh, but print returns Nil, is that it? Hmmm.
I’m thinking in Shell I suppose! Or Icon…
Hmmm. I always get tangled with these. Bracketing things doesn’t help
You could make a virtue of necessity and decide that lines like this
should start with ! to draw attention to themselves:
error_msg = “error”
error = true
! puts error_msg and exit 1 if error
[…]
Interesting… Only I’d be bound to do this for something that doesn’t return nil, and get completely confused debugging it!
I think I prefer multiple lines, and use folding in the editor to tidy
it away.
I suppose this is more to do with how one uses a program to communicate
with others who read it, rather than with the computer. Interesting
thread from such a simple question.
David
–
David Alan Black
Hugh
···
On Thu, 18 Jul 2002, David Alan Black wrote:
On Thu, 18 Jul 2002, Hugh Sasse Staff Elec Eng wrote:
–
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)
I learned that there are many way to do one task.
I adopted
.==================================================
(print error_msg; exit 1) if error_flag
.==================================================
because it is the easiest way to understand, I think.