
单身狗运算符
As of Ruby 2.3.0 there is a new operator known as the Safe Navigation Operator, or the Lonely Operator. According to Matz it “looks like someone sitting on the floor, looking at the dot.” What this operator allows you to do is continue chaining methods even if one of the items along the way returns nil. It will safely return nil if that is the case.
1 |
@a&.size |
Notice we didn’t get a NoMethodError. This will save a lot of the uses of the Rails try method.




近期评论