Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
对于a = a || b, a = a && b, 有很好的语法糖a ||= b, a &&= b. 也经常出现的是:
a = a || b
a = a && b
a ||= b
a &&= b
a = b || a a = b && a
有没有好的语法糖或写这个的简短方法?
a = b || a
可以改写为:
a = b if b
可以在不重复a或b的情况下重写
a
b
x = b and a = x
如果表达式a和b都超过 6 个字符,x = b and a = x则为最短形式。
目前没有这样的捷径。
&&= 和 ||=在 CRuby/MRI AST 中是NODE_OP_ASGN_AND和NODE_OP_ASGN_OR 。仅当左值为 false 或 nil 时才评估“右值”。