Is there any wisdom behind “and”, “or” operators in Ruby?

前端 未结 5 1076
小蘑菇
小蘑菇 2020-11-29 11:21

I wonder why ruby give and, or less precedence than &&, || , and assign operator? Is there any reason?

5条回答
  •  攒了一身酷
    2020-11-29 11:57

    Being able to control the precedence of your operators is sometimes useful, especially if you are concerned with readability -- extra parenthesis in conditional statements can sometimes obscure the actual logic.

    To be frank, though, I think the reason Ruby has the boolean operator precedence levels it does stems mostly from the fact that Matz was a Perl programmer before he ever wrote Ruby, and borrowed much of the core syntax and operators from that language.

提交回复
热议问题