ruby and 和 && 是不同的

http://www.themomorohoax.com/2008/12/08/and-vs-amperand-in-ruby

>> alien = true
>> speaks_english = false

# 1
>> alien and speaks_english ? 'hello' : '**silence**'
=> "**silence**"

# 2
>> alien && speaks_english ? 'hello' : '**silence**'
=> "**silence**"

# 3
>> speaks_english and alien ? 'hello' : '**silence**'
=> false  # oops

# 4
>> speaks_english && alien ? 'hello' : '**silence**'
=> "**silence**"

>> hammer_of_truth = true
>> anvil_of_false = false

>> sword = hammer_of_truth && anvil_of_false
=> false
>> sword
=> false

>> sword = hammer_of_truth and  anvil_of_false
=> false
>> sword
=> true # oops

 

注:在ruby中,and,or,not的优先级最低.


转自:  https://ruby-china.org/topics/3898

你可能感兴趣的:(ruby and 和 && 是不同的)