为什么Ruby同时拥有&&和'and'运算符?

Ali*_*air 1 ruby language-design operators

我理解他们之间的区别,但我无法理解为什么他们都被包含在语言中.当然两者都会造成混乱?

Ser*_*sev 5

它们的优先级不同,所以它们不相同.

我的经验法则如下:&&用于逻辑表达式并and用于控制流程.

例子

# logical expressions
if user.first_name == 'Bob' && user.last_name == 'Jones'

# control flow
worker.do_this and worker.and_also_do_this_if_that_went_well
Run Code Online (Sandbox Code Playgroud)