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?

On Wed, Oct 20, 2010 at 10:26 PM, Courtland A.
[email protected] wrote:

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

ri any?

At 2010-10-20 10:26AM, “Courtland A.” wrote:

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

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

On Oct 20, 9:26am, Courtland A. [email protected] wrote:

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


Courtland A.

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 20 out, 12:34, botp [email protected] wrote:

On Wed, Oct 20, 2010 at 10:26 PM, Courtland A.

[email protected] wrote:

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

ri any?

see what a concise and precise answer is, waxhead?

On 20.10.2010 16:34, botp wrote:

On Wed, Oct 20, 2010 at 10:26 PM, Courtland A.
[email protected] 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 10/20/2010 07:34 AM, botp wrote:

On Wed, Oct 20, 2010 at 10:26 PM, Courtland A.
[email protected] wrote:

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

ri any?

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

On Wed, Oct 20, 2010 at 11:25 AM, Robert K.
[email protected]wrote:

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

Courtland A. 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

On Oct 20, 2:39pm, Robert K. [email protected] wrote:

Not much of an improvement though.

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

On 20.10.2010 20:37, Courtland A. wrote:

On Wed, Oct 20, 2010 at 11:25 AM, Robert K.
[email protected]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.

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

Not much of an improvement though.

Cheers

robert

On Wed, Oct 20, 2010 at 10:32 PM, Shadowfirebird
[email protected] wrote:

Not to everyone’s taste, I know…

That’s not the same as “something if a || b || c || d”!

robert

Excerpts from Robert K.'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.

On Oct 20, 4:24pm, Shadowfirebird [email protected] wrote:

I might resort to:

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

something() if complextest

Or I might even make complextest a function.

+1

On Thu, Oct 21, 2010 at 3:40 AM, Robert K.
[email protected] wrote:

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

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

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…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs