Long conditional statements

Some parts of my code call for really long conditional statements of the
form:

if a or b or c or d or e or f

  doSomething()

end

Is there a more aesthetically-pleasing way to accomplish this?

···

--
*Courtland Allen*

ri any?

···

On Wed, Oct 20, 2010 at 10:26 PM, Courtland Allen <courtlandallen@gmail.com> wrote:

if a or b or c or d or e or f

Use case/end
Assuming your expressions are long, you can add newlines as required:

    case
    when
        a,
        b,
        c,
        d,
        e,
        f
      doSomething
    else
      doOtherThing
    end

···

At 2010-10-20 10:26AM, "Courtland Allen" wrote:

[Note: parts of this message were removed to make it a legal post.]

Some parts of my code call for really long conditional statements of the
form:

if a or b or c or d or e or f

   doSomething()

end

Is there a more aesthetically-pleasing way to accomplish this?

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

x = 4 ; y = 9 ; z = -2
    ==>-2
def do_something
  puts "Something done."
end
    ==>nil
if x.odd? or
   y.even? or
   z < 0
  do_something
end
Something done.
    ==>nil

x.even? and y.odd? and puts "Done."
Done.
    ==>nil

···

On Oct 20, 9:26 am, Courtland Allen <courtlandal...@gmail.com> wrote:

[Note: parts of this message were removed to make it a legal post.]

Some parts of my code call for really long conditional statements of the
form:

if a or b or c or d or e or f

doSomething()

end

Is there a more aesthetically-pleasing way to accomplish this?

--
*Courtland Allen*

Courtland Allen wrote in post #955781:

Some parts of my code call for really long conditional statements of the
form:

if a or b or c or d or e or f

  doSomething()

end

Is there a more aesthetically-pleasing way to accomplish this?

You can split them after the operator and continue them on the next
line, without needing any explicit continuation character.

if a or
   b or
   c or
   d or
   e or
   f
  doSomething
end

···

--
Posted via http://www.ruby-forum.com/\.

see what a concise and precise answer is, waxhead?

···

On 20 out, 12:34, botp <botp...@gmail.com> wrote:

On Wed, Oct 20, 2010 at 10:26 PM, Courtland Allen > > <courtlandal...@gmail.com> wrote:
> if a or b or c or d or e or f

ri any?

Caution: #any? does not short-circuit evaluation.

···

On 10/20/2010 07:34 AM, botp wrote:

On Wed, Oct 20, 2010 at 10:26 PM, Courtland Allen > <courtlandallen@gmail.com> wrote:

if a or b or c or d or e or f

ri any?

Problem is that if a, b, c, d, e and f are expressions you would have to evaluate them beforehand thus loosing short circuit evaluation:

if [a,b,c,d,e,f].any? {|x| x}
   ...
end

I don't even find that more aesthetically pleasing. I'd probably rather refactor, e.g.

if complex_condition(x,y)
   do_something
end

Courtland, can you demonstrate a real example of your complex conditions? Maybe there are other ways to transform them and make them less complex.

Cheers

  robert

···

On 20.10.2010 16:34, botp wrote:

On Wed, Oct 20, 2010 at 10:26 PM, Courtland Allen > <courtlandallen@gmail.com> wrote:

if a or b or c or d or e or f

ri any?

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

Call me odd, but I quite like:

    def something()
        ...
    end

    something() if a
    something() if b
    something() if c
    something() if d

Not to everyone's taste, I know...

···

--
There are two types of people in this world, good and bad. The good
sleep better, but the bad seem to enjoy the waking hours much more.
    -- Woody Allen

Courtland, can you demonstrate a real example of your complex conditions?
Maybe there are other ways to transform them and make them less complex.

Cheers

       robert

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

It happens in a variety of different ways in my code, so the more techniques
you guys list, the better (I hadn't known about any?).

But here's one example anyway:

if somehash[:key1].blank? or somehash[:key2].blank?

or somehash[:key2].blank? or somehash[:key2].blank?

  doSomething()

end

···

On Wed, Oct 20, 2010 at 11:25 AM, Robert Klemme <shortcutter@googlemail.com>wrote:

That's not the same as "something if a || b || c || d"!

robert

···

On Wed, Oct 20, 2010 at 10:32 PM, Shadowfirebird <shadowfirebird@gmail.com> wrote:

Call me odd, but I quite like:

def something()
...
end

something() if a
something() if b
something() if c
something() if d

Not to everyone's taste, I know...

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

if [:key1, :key2].any? {|k| somehash[k].blank?}
   do_something
end

Not much of an improvement though.

Cheers

  robert

···

On 20.10.2010 20:37, Courtland Allen wrote:

On Wed, Oct 20, 2010 at 11:25 AM, Robert Klemme > <shortcutter@googlemail.com>wrote:

Courtland, can you demonstrate a real example of your complex conditions?
  Maybe there are other ways to transform them and make them less complex.

It happens in a variety of different ways in my code, so the more techniques
you guys list, the better (I hadn't known about any?).

But here's one example anyway:

if somehash[:key1].blank? or somehash[:key2].blank?

or somehash[:key2].blank? or somehash[:key2].blank?

   doSomething()

end

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

Excerpts from Robert Klemme's message of Wed Oct 20 21:44:25 +0100 2010:

That's not the same as "something if a || b || c || d"!

Arse. No, of course not. Sorry, long day.

I don't think I can beat

    something() if (a || b || c || d)

The important thing, I think, is to factor out the conditional code into something simple to make the whole statement more readable. But of course if I had

    something() if (a || (b && c || a && c))

I might resort to:

    complextest = (a || (b && c || a && c)

    something() if complextest

Or I might even make complextest a function.

if somehash.values_at( :key1, :key2 ).index( "" )
  do_something
end

···

On Oct 20, 2:39 pm, Robert Klemme <shortcut...@googlemail.com> wrote:

On 20.10.2010 20:37, Courtland Allen wrote:

> On Wed, Oct 20, 2010 at 11:25 AM, Robert Klemme > > <shortcut...@googlemail.com>wrote:
>> Courtland, can you demonstrate a real example of your complex conditions?
>> Maybe there are other ways to transform them and make them less complex.
> It happens in a variety of different ways in my code, so the more techniques
> you guys list, the better (I hadn't known about any?).

> But here's one example anyway:

> if somehash[:key1].blank? or somehash[:key2].blank?
>> or somehash[:key2].blank? or somehash[:key2].blank?

> doSomething()

> end

if [:key1, :key2].any? {|k| somehash[k].blank?}
do_something
end

Not much of an improvement though.

+1

···

On Oct 20, 4:24 pm, Shadowfirebird <shadowfireb...@gmail.com> wrote:

I might resort to:

complextest = \(a || \(b &amp;&amp; c || a &amp;&amp; c\)

something\(\) if complextest

Or I might even make complextest a function.

do_something if [:key1, :key2].any? {|k| somehash[k].blank?}

···

On Thu, Oct 21, 2010 at 3:40 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

if [:key1, :key2].any? {|k| somehash[k].blank?}
do_something
end