[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can this code be more succinct

RichardOnRails

4/29/2009 1:45:00 AM

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
18 Answers

Joel VanderWerf

4/29/2009 2:04:00 AM

0

RichardOnRails wrote:
> 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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

7stud --

4/29/2009 2:06:00 AM

0

RichardOnRails wrote:
> 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



--
Posted via http://www.ruby-....

RichardOnRails

4/29/2009 3:38:00 AM

0

On Apr 28, 9:45 pm, RichardOnRails
<RichardDummyMailbox58...@USComputerGurus.com> wrote:
> 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....

With gratitude,
Richard

Robert Klemme

4/29/2009 5:56:00 AM

0

On 29.04.2009 05:38, RichardOnRails wrote:
> On Apr 28, 9:45 pm, RichardOnRails
> <RichardDummyMailbox58...@USComputerGurus.com> wrote:
>> 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....

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


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Marc Heiler

4/29/2009 10:37:00 AM

0

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.
--
Posted via http://www.ruby-....

Mark Thomas

4/29/2009 12:32:00 PM

0

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.

botp

4/29/2009 11:32:00 PM

0

On 4/30/09, Mark Thomas <mark@thomaszone.com> wrote:
> 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

7stud --

4/30/2009 6:31:00 AM

0

botp wrote:
> On 4/30/09, Mark Thomas <mark@thomaszone.com> wrote:
>> 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"
--
Posted via http://www.ruby-....

7stud --

4/30/2009 6:34:00 AM

0

7stud -- wrote:
>
> ...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". :)
--
Posted via http://www.ruby-....

Michael Ejercito

4/1/2014 2:48:00 AM

0


"plainolamerican" <plainolamerican@gmail.com> wrote in message
news:6a2862b6-1ec2-4ee3-817a-acad0570ca0e@googlegroups.com...
> On Sunday, March 30, 2014 11:44:58 PM UTC-5, Heinrich wrote:
>> http://www.europeandailynews.org/2014/03/30/jewish-journalist-europeans-close-to-becoming-mentally-ill-with-ant...
>
> Europeans close to becoming mentally ill with antisemitism
> ---
> there's a change a comin!!!
Not for the better.


Michael