ruby - `&&=` 的用例

标签 ruby syntactic-sugar

我在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/

相关文章:

ruby-on-rails - Ruby 中的 block 是否被过度使用?

haskell - cons操作cons元素是从右到左吗?

scala - :_* convert ordered collections into variable arg lists? 怎么样

javascript - 返回内部 if else 并使用粗箭头

python - 将@property 方法添加到类

ruby-on-rails - 使用&调用和接收参数的区别

ruby-on-rails - rails 中的动态路线

ruby-on-rails - 简化的 'logical' 模型可以干净地访问高度规范化的数据库

ruby-on-rails - 在 Substruct 中设置 Paypal 帐户

c# - Web Api 中的语法糖 - 如何省略 [FromBody]