Can this code be more succinct

R

RichardOnRails

Hi All,

I like to use the following form for handling error situations:

( puts "ERROR: Long msg";
exit) if some_boolean _expression

I want the multiple form because the error messages are often long and
sometimes multi-line.

Is there any way to improve it by getting rid of the parentheses?

Thanks in advance,
Richard
 
J

Joel VanderWerf

RichardOnRails said:
Hi All,

I like to use the following form for handling error situations:

( puts "ERROR: Long msg";
exit) if some_boolean _expression

I want the multiple form because the error messages are often long and
sometimes multi-line.

Is there any way to improve it by getting rid of the parentheses?

if some_boolean_expression
puts "ERROR: Long msg"
exit
end #SCNR


Seriously, though, what about this:

abort "ERROR: Long msg " +
"more message" if true

or

abort %{
line 1
line 2
} if true
 
7

7stud --

RichardOnRails said:
Hi All,

I like to use the following form for handling error situations:

( puts "ERROR: Long msg";
exit) if some_boolean _expression

I want the multiple form because the error messages are often long and
sometimes multi-line.

Is there any way to improve it by getting rid of the parentheses?

Thanks in advance,
Richard


Why use such a tortured construct in the first place? This is much
clearer:


if condition
puts <<-ENDOFSTRING
warning error warning error warning error warning error
warning error warning error warning error warning error
warning error warning error warning error warning error
warning error warning error warning error warning error
warning error warning error warning error warning error
ENDOFSTRING

exit
end
 
R

RichardOnRails

Hi All,

I like to use the following form for handling error situations:

(   puts "ERROR: Long msg";
    exit)  if some_boolean _expression

I want the multiple form because the error messages are often long and
sometimes multi-line.

Is there any way to improve it by getting rid of the parentheses?

Thanks in advance,
Richard

Thank you both for your generous, expert advice.

Here are two statements I now have in my app instead of my former,
convoluted and heavily parenthesizes statements:

http://www.pastie.org/462055

With gratitude,
Richard
 
R

Robert Klemme

Thank you both for your generous, expert advice.

Here are two statements I now have in my app instead of my former,
convoluted and heavily parenthesizes statements:

http://www.pastie.org/462055

Frankly, I find this bit still quite convoluted, especially because of
the assignment in "unless". How about:

arg = ARGV.shift || 'os'
abort "ERROR:..." unless /\Aos\z/i =~ arg
abort "ERROR: multiple..." unless ARGV.empty?

Kind regards

robert
 
M

Marc Heiler

I can't help but feel that instead of getting more succint it seems to
become more and more verbose ... ;)

Except for the one-liner abort, which looks quite nice.
 
M

Mark Thomas

I can't help but think this could be wrapped up in a nice, neat case
statement that is perhaps not more succinct but more readable.

case arg = ARGV[0]
when /^os$/
sym = arg.downcase
else
abort "ERROR: invalid arg #{arg}"
end

but I'm not sure whether the number of args check can be handled
inside the case. I tried a 'when ARGV.size > 1' but you can't put a
full expression in a when statement.
 
B

botp

but I'm not sure whether the number of args check can be handled
inside the case. I tried a 'when ARGV.size > 1' but you can't put a
full expression in a when statement.

there's another form for case.
try this (untested),

arg = ARGV[0]
case
when ARGV.size > 1
abort "too many args ;-)"
when /^os$/ =~ arg
sym = arg.downcase
else
abort "ERROR: invalid arg #{arg}"
end
 
7

7stud --

botp said:
but I'm not sure whether the number of args check can be handled
inside the case. I tried a 'when ARGV.size > 1' but you can't put a
full expression in a when statement.

there's another form for case.
try this (untested),

arg = ARGV[0]
case
when ARGV.size > 1
abort "too many args ;-)"
when /^os$/ =~ arg
sym = arg.downcase
else
abort "ERROR: invalid arg #{arg}"
end

...but then instead of having to look up the format of a case statement,
you could just write:

val = "red"

if val == "blue"
puts "blue"
elsif val == "red"
abort "code red"
else
puts "not recognized"
end

puts "program terminating normally"
 
7

7stud --

7stud said:
...but then instead of having to look up the format of a case statement,
you could just write:

val = "red"

if val == "blue"
puts "blue"
elsif val == "red"
abort "code red"
else
puts "not recognized"
end

puts "program terminating normally"

...but then it is easier to type "when" than
"elseif<backspace><backspace><backspace>if". :)
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top