我在Ruby代码中看到和使用过很多||=
,但是在实际应用中我几乎没有看到或使用过&&=
。 &&=
有用例吗?
最佳答案
不是油嘴滑舌,但明显的用例是任何时候你会说 x && foo
并希望将结果存储回 x
。这是一个:
list = [[:foo,1],[:bar,2]]
result = list.find{ |e| e.first == term }
result &&= result.last # nil or the value part of the found tuple
关于ruby - `&&=` 的用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989361/