Q: "print usage_msg, exit 1 if error_flag" does not work

Hi,

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
.========================================

How to write it in ruby? Please tell me.

regards,
kwatch

    print usage_msg, exit 1 if error_flag

       print usage_msg or exit 1 if error_flag

Guy Decoux

print usage_msg, exit 1 if error_flag
   print usage_msg or exit 1 if error_flag

That should be “and” rather than “or” for both to be done if error_flag
??

Guy Decoux

    Hugh
···

On Thu, 18 Jul 2002, ts wrote:

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%

Guy Decoux

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.

Guy Decoux

    Hugh
···

On Thu, 18 Jul 2002, ts wrote:

Hi –

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

:slight_smile:

David

···

On Thu, 18 Jul 2002, Hugh Sasse Staff Elec Eng wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Array syntax works as sequential control, e.g.,

[ print(error_message), exit(1) ] if error_flag

error_flag and [ print(error_message), exit(1) ]

if you could accept these unconventional application :slight_smile:

– 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…

Hi –

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! :slight_smile:

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:

GOTO Kentaro wrote:

[snip]

Array syntax works as sequential control, e.g.,

[ print(error_message), exit(1) ] if error_flag

error_flag and [ print(error_message), exit(1) ]

if you could accept these unconventional application :slight_smile:

Or,

(print error_message; exit 1) if error_flag

Hi –

Hi –

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! :slight_smile:

Yeah, I wouldn’t recommend actually using it. Kind of too bad – it’s
almost really cool :slight_smile:

I think I prefer multiple lines, and use folding in the editor to tidy
it away.

Me too, or perhaps an assert method of some kind.

David

···

On Fri, 19 Jul 2002, Hugh Sasse Staff Elec Eng wrote:

On Thu, 18 Jul 2002, David Alan Black wrote:

On Thu, 18 Jul 2002, Hugh Sasse Staff Elec Eng wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Since this thread still hasn’t died, I’ll throw another
alternative on the heap:

proc { |msg| print msg; exit 1 }.call(error_message) if error_flag

– Dossy

···

On 2002.07.19, Mark Slagell ms@iastate.edu wrote:

(print error_message; exit 1) if error_flag


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)

Mark Slagell ms@iastate.edu writes:

(print error_message; exit 1) if error_flag

Or, if this is something that happens more than once, perhaps express
the intent:

def fatal_error(msg)
puts msg
exit 1
end

fatal_error(error_message) if error_flag

fatal_error(another_message) if another_flag

Thanks to all who answered my question.

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.

regards,
kwatch

Mark Slagell ms@iastate.edu wrote in message news:3D36F1E8.1040605@iastate.edu

···

GOTO Kentaro wrote:

[snip]

Array syntax works as sequential control, e.g.,

[ print(error_message), exit(1) ] if error_flag

error_flag and [ print(error_message), exit(1) ]

if you could accept these unconventional application :slight_smile:

Or,

(print error_message; exit 1) if error_flag

Hi,

···

At Fri, 19 Jul 2002 01:54:55 +0900, Dave Thomas wrote:

(print error_message; exit 1) if error_flag

Or, if this is something that happens more than once, perhaps express
the intent:

def fatal_error(msg)
puts msg
exit 1
end

abort in 1.7 does it.

$ ruby-1.7 -e ‘abort “Hi!”’; echo $?
Hi!
1


Nobu Nakada

kwatch@lycos.jp (kwatch) writes:

I learned that there are many way to do one task.

This is a good lesson to be learned :slight_smile:

···


Josh Huber