“and” and “or” are like backwards statement modifiers

It occurred to me after reviewing all the feedback to my last post that despite making the connection to statement modifiers, I never directly contrasted the two. And that this might be an even simpler way of explaining the usefulness of and/or in Ruby. I’ve added the following examples to the original post, but in case you missed it:

and

You can think of and is as a reversed if statement modifier:

  next if widget = widgets.pop

becomes:

  next if widget = widgets.pop

Because of the low precedence of and, no extra parentheses are needed here as they would be for &&.

or

Likewise, or is a reversed unless statement modifier:

  raise "Not ready!" unless ready_to_rock?

becomes:

  ready_to_rock? or raise "Not ready!"

Conclusion

Which version to use – if/unless or and/or – depends on your taste and what reads well in a given case. But it’s nice to have the option.

[ad#PostInline]

6 comments

  1. Of course, the if/unless and and/or relation is obvious when you know a little bit of logic:
    (a and b) == ((not a) or b) and (a or b) == ((not a) and b)

    1. Rubbish. For example, the statement “(A and B) = ((not A) or B)” is false when A is false and B is true. That is because the left-hand side if false, but the right-hand side is true.

      1. Woops, moment of befuddlement.
        ((not A) or B) is equivalent implication.
        What I meant to be quoting were the DeMorgan rules.

        The connection make a lot more sense combined with if/unless.

        (!(a ^ b)) == ((!a) v (!b))

  2. 大人気を海外激安通販専門店ロレックス、ブルガリ、フランク ミュラー、シャネル、カルティエ、オメガ、IWC、ルイヴィトン、オーデマ ピゲ、ブライトリング、グッチ、エルメス、パネライ、パテックフィリップ偽物(コピー商品)のブランドの腕時計の販売、通販。ブランドコピー激安、ブランドコピー時計、ブランドコピー品当店のスーパーコピー商品は他店よりも質が高く、金額も安くなっております。ご購入する度、ご安心とご満足の届けることを旨にしております http://www.bagkakaku.com/vuitton_wallet/wallet/145.html

  3. 人気の春夏シャネルベルトコピー流行ファッションや定番アイテムなどファッションアイテムが勢揃いシャネル CHANEL レディースファッション ファッション雑貨・小物 ベルト を海外通販!美しいシャネルのサングラス、格安のシャネルのサングラスコピーこれらの魅力的な割引シャネルのサングラスだけでなく、暑い夏に目を保護することも するのに十分な美しさと優雅さを追加します。シャネル サングラス カメリア・最安価格をぶった斬り! http://www.ooowatch.com/tokei/chanel

Leave a Reply

Your email address will not be published. Required fields are marked *