-1

I have a code where I want an if statement with 3 conditions passed. However, in one of my conditions, I can allow either or.

For example, the if statement is true if( 5 && 6 && (7||9||10||12)). So 5,6,10 would be true, but 5,6,13 wouldn't.

I have a code in ruby with this similar logic but it is giving me syntax errors (maybe because of an extra parenthesis). However, I was wondering if this logic is allowed.

if (constraintsHashed[i][DINING_CONSTRAINTS].downcase.include? userDiningOptions) && (constraintsHashed[i][COST_CONSTRAINTS].downcase.include? userBudget) && 
(((userTime >= Time.parse(TENAM)) && userTime <= Time.parse(NINEPM)) or (userTime >= Time.parse(SEVENAM) && userTime <= Time.parse(TWELVEAM)) or 
(userTime >= Time.parse(SEVENAM)) && (userTime <= Time.parse(FIVEPM)) or (userTime >= Time.parse(NINEAM)) && (userTime <= Time.parse(SIXPM)) or 
(userTime >= Time.parse(TWELVEPM)) && (userTime <= Time.parse(TWELVEAM)) or (userTime >= Time.parse(TENAM)) && (userTime <= Time.parse(ELEVENPM)))

Stack trace:

diningHall.rb:132: syntax error, unexpected keyword_else, expecting ')'
diningHall.rb:134: syntax error, unexpected keyword_end, expecting ')'
                end             #End for if(constraintsHashed
                   ^
diningHall.rb:141: syntax error, unexpected keyword_end, expecting ')'
end                                     #End for def weekendOptions
   ^
diningHall.rb:269: syntax error, unexpected end-of-input, expecting ')'
main()                          #Program starts here
                                                    ^
Andronicus
  • 25,419
  • 17
  • 47
  • 88
Ruby
  • 1
  • Check that your parentheses are balanced. The error suggests you're missing a `)`. – lurker Feb 10 '19 at 03:37
  • Also, I see you're using the keyword `or` instead of `||`, make sure you know what the [differences between those](https://stackoverflow.com/questions/2083112/difference-between-or-and-in-ruby) are. It's a common pitfall – Simple Lime Feb 10 '19 at 04:17

2 Answers2

0

Logic of this kind is definitely allowed in Ruby. The parentheses error are probably caused by a parentheses mismatch. To make it easier to debug, I would advise that you use in-built Ruby abstractions to handle comparisons like this. For example, in your particular case, instead of having to use userTime <= Time.parse(something) && userTime <= Time.parse(somethingElse), you could use the in-built ruby method of Time.parse(userTime).between?(Time1, Time2) where Time1, Time2 are pre-defined. This will eliminate parentheses, make the code clearer, take up less space, and help you debug your logic better.

SalmonKiller
  • 2,183
  • 4
  • 27
  • 56
0

You have added extra brackets in the condition which is causing syntax error. Try below condition

      if (constraintsHashed[0][DINING_CONSTRAINTS].downcase.include? userDiningOptions) && 
      (constraintsHashed[0][COST_CONSTRAINTS].downcase.include? userBudget) && 
      ((userTime >= Time.parse(TENAM) && userTime <= Time.parse(NINEPM)) or 
        (userTime >= Time.parse(SEVENAM) && userTime <= Time.parse(TWELVEAM)) or 
        (userTime >= Time.parse(SEVENAM)) && (userTime <= Time.parse(FIVEPM)) or 
        (userTime >= Time.parse(NINEAM)) && (userTime <= Time.parse(SIXPM)) or 
        (userTime >= Time.parse(TWELVEPM)) && (userTime <= Time.parse(TWELVEAM)) or 
        (userTime >= Time.parse(TENAM)) && (userTime <= Time.parse(ELEVENPM)))

Hope this help!!