Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Multi-line conditional syntax: how do I do it?

Tags:

syntax

ruby

What I'm trying to do:

result = (not question?) \
          and ( \
            condition \
            or ( \
              comparer == compared and another_question? \ 
            ) \
          )   

The goal is to have complicated and / or logic and still have it be readable.

The problem with the above attempted syntax is that it some how messes up parenthesis in ruby's parser, so console says that the error is in a file that this code isn't in. (though it's in the call stack)

without the back slashes, I get these:

syntax error, unexpected kAND, expecting kEND (SyntaxError)

and

 syntax error, unexpected kOR, expecting ')'

any ideas on how to properly do this?

like image 918
NullVoxPopuli Avatar asked Aug 26 '11 21:08

NullVoxPopuli


Video Answer


1 Answers

Remove the space after the backslash in another_question? \. You're escaping the space rather than the newline, which causes a syntax error.

Note you don't need to escape every newline.

result = (not question?) \
          and (
            condition \
            or (
              comparer == compared and another_question?
            )
          ) 
like image 186
outis Avatar answered Nov 13 '22 21:11

outis