Back to Course

And/Or

Module 4, Lesson 1Next

Responses

  1. What I enjoy most of all in RubyTapas is the depth to which Avdi dives into a topic. This And/or topic looks quite simple from a first glance: never use them. However in the episode some history has been brought, so we understand why they’re here. And also clear example provided when and/or better than their logical counterparts.
    To be fair, I just wanted to say thank you once again. Didn’t have enough time to write shorter message:)

  2. Honestly, this video was great, because the english version of “and/or” were never even discussed during my bootcamp months, so this is all brand new information to me! It was just assumed that we would use || and &&. So that’s fun 😁

  3. I didn’t know about english version of || and &&. I’m not sure really what I learned here hahaha
    Maybe to be aware of its existence if I find it someplace, and that its better to stick to what I know for booleans (|| and &&) and if/else and other flow controls.

  4. I’ve seen this syntax before, but only in books/tutorials. It’s always been presented as synonymous with the symbolic versions, so it was great to get a deeper dive into how different the two really are.

    I think it leaves me wondering if I’ll be able to develop the eye to recognize when these keywords should be used when I’m actually writing code.

    1. That’s a great point.

      I think the thing to keep in mind is that you can get away with ignoring the English versions entirely. For boolean operations the symbolic versions are always the right choice, and for flow control you can always just use if or unless. Return to and and or only if there’s an if or unless construct that just feels awkward to you and you’re looking for a way to restate it.

  5. Wow! This is good to know. The Rubocop gem for rails advocates for the logical operators (and will change all of the english ones if you, like me, are in the habit of running “rubocop -a”). This explains a bug or two wherein I was following some documentation to implement another gem, the documentation of which used “or,” and then rubocop -a switched it to “||.” You just saved me a lot of debugging. I am so excited!

  6. This lesson motivated me to read more about control flow operators and it also surprised me because I did not know about the English versions of and/or. I enjoyed the lesson because it encouraged me to dive deeper in to things I think I would maybe overlook, just like “and” and “or”. I think I’ve seen them both used before online but I think I just brushed it off because I assumed it was just like the symbolic && and ||, but now I know that I should challenge my assumptions and take some time to examine things more.

  7. After the lesson, it made be want to dive more into control flows. In my time working with Ruby, I also have come to favor using “statement unless boolean” or “statement if boolean” type layout with evaluating logic.

    Also it is kind of annoying to always be taken away from the lesson I’m working on when I click ” Continue the discussion the forums” at the bottem of the lesson page. Could this open in a new tab instead of using the tab in use? I feel like I am constantly having to reopen the course page.

  8. This lesson immediately validated my initial instinct to sign up for this course! I found this particularly interesting, especially looking at how you can use and rather than nested if statements. I now have an urgency to write a blog post about object precedence and scour an open-source project for control flow improvements.

    Overall, I’m looking forward to this topic. I find it hard sometimes to handle control flow as it relates to exceptions and errors. Sometimes I find it difficult to switch back to a philosophy of Ruby and OOP languages in general, which says to fail silently, compared to Erlang, which encourages you to fail often and loudly.

  9. This is a great explanation of how to use “and”/”or” for control flow! I’ll mention one small hiccup that I ran into when I started using these keywords. I tried writing a function similar to this:

    puts 'foo' and return false if arg == :bar

    But it was my surprise when the console output was there but the function result was wrong!

    Looking back, I realized that puts returns nil, so the and logic stopped there. I learned that I still have to be careful of the expected values for each part of the statement!

    1. That’s a great point! It’s easy to think of “and” in terms of English “do this, then this” when really it’s “do this, and if that worked, do this other thing”.

      What you’re looking for is something like C’s “sequential operator” (the comma). Some Ruby options include:

      begin puts 'foo'; return false end if arg == :bar
      
      (puts 'foo'; return false) if arg == :bar
      
      puts('foo').then{ return false } if arg == :bar
      
  10. This is really great because generally and / or has been (in the last while) frowned upon, and this gives a great case for it immediately for control flow (and NOT return values!)!