Sunday, January 22, 2017

No Difference Between Boolean Operators AND and OR in Case Conditional in Ruby

I can't see a practical difference between the boolean operators AND and OR in a Ruby case conditional.

For example, I want to get the user to input the sentence:

sudo make me a sandwich

And the case conditional starts as follows:

case user_selection
when /sudo/ && /sandwich/

However, if the user enters:

make me a sandwich

The condition will be a satisfied.

My way around it in this instance is to re-order the conditions:

case user_selection
when /sandwich/ && /sudo/

But that pre-supposes that every time a user thinks to use "sudo" he will include the string "sandwich" in his response.  However, this is functionally no different from this:

case user_selection
when /sudo/

I looked up boolean operators for Ruby conditionals, but have not found a satisfactory answer.

2 comments:

  1. Case statements evaluate a single variable, and check if it matches the when argument.

    When [expression]
    Does not work like an if conditional, executing the block if the expression is true. Instead it works more like an index for matches.

    ReplyDelete
    Replies
    1. Think...

      If ( [expression] ==

      [possible match] ) then

      [possible match] ) then

      Delete

1,050 hours

It took me 13 working days to complete my first 100 "work" pomodoros as a Junior Software Tester at Profectus Group.  Much of ...