Ruby中的“|| =”运算符
发布时间:2020-12-16 19:42:17 所属栏目:百科 来源:网络整理
导读:有人可以向我解释以下 Ruby代码的含义吗? (我在一个人的项目中看到这个代码片段): car ||= (method_1 || method_2 || method_3 || method_4) 以上代码与以下代码有什么区别? car = method_1 || method_2 || method_3 || method_4 ———-更新————–
有人可以向我解释以下
Ruby代码的含义吗? (我在一个人的项目中看到这个代码片段):
car ||= (method_1 || method_2 || method_3 || method_4) 以上代码与以下代码有什么区别? car = method_1 || method_2 || method_3 || method_4 ———-更新————– 好的,在读取@ Dave的解释后,我得到了|| =运算符的含义,我的下一个问题是如果method_2,method_3和method_4都返回一个值,哪个值将被分配给car? (假设汽车最初没有) 解决方法
它是“条件分配”的赋值操作符
看到这里 – > http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators 条件分配: x = find_something() #=>nil x ||= "default" #=>"default" : value of x will be replaced with "default",but only if x is nil or false x ||= "other" #=>"default" : value of x is not replaced if it already is other than nil or false 运算符|| =是表达式的简写形式: x = x || "default" 编辑: 看完OP的编辑之后,这个例子只是这个的扩展,意思是: car = method_1 || method_2 || method_3 || method_4 将将method_1,method_2,method_3,method_4(按顺序)分配给汽车的第一个非零或非返回值,否则将保留其旧值. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Oracle当客户端与Oracle服务创建连接后
- 1. PostgreSQL安装详细步骤(windows)
- c# – 从绝对路径获取流?
- ajax或者jQuery的ajaxSubmit出现请求根本没有发出的问题
- c# – 为什么在LINQ查询之后的GC释放了WhereListIterator,而
- React Native 每日一学(Learn a little every day)
- swift3 – Cocoapods | podspec将GoogleAnalytics添加为依赖
- 使用xml写一个关于textSize的selector
- ruby-on-rails – 与另一个控制器的before_filter
- Cocos2d-X 学习笔记 11 制作Loading视图