Forum: Ruby Can this code be more succinct

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 RichardOnRails (Guest)
on 2009-04-29 03:50
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-04-29 04:05
(Received via mailing list)
RichardOnRails wrote:
> 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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-29 04:05
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
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 RichardOnRails (Guest)
on 2009-04-29 05:40
(Received via mailing list)
On Apr 28, 9:45 pm, RichardOnRails
<RichardDummyMailbox58...@USComputerGurus.com> wrote:
> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-29 08:01
(Received via mailing list)
On 29.04.2009 05:38, RichardOnRails wrote:
>> sometimes multi-line.
>
> 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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-04-29 12:36
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.
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2009-04-30 00:16
(Received via mailing list)
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.
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2009-04-30 01:33
(Received via mailing list)
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-30 08:31
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"
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-30 08:33
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".  :)
This topic is locked and can not be replied to.