ruby – 带括号和没有括号的方法调用的优先级是什么?
以前的答案
answer到类似的question是错误的. Ruby documentation和community wiki都没有提到方法调用. 没有括号的方法调用 高于或 或者似乎比没有括号的方法调用具有更低的优先级: puts false or true 相当于 ( puts false ) or true 并显示错误. 注意:我知道或不应该使用.不过,这是一个很好的例子,表明某些运算符的优先级低于方法调用. 低于|| puts false || true 相当于 puts (false || true) 并显示真实. 用括号调用方法 用于方法调用don’t seem的括号分组: puts(false or true) # SyntaxError: unexpected keyword_or puts((false or true)) #=> true 题 在有和没有括号的方法调用应该在table的优先级中的哪个位置? 赏金澄清 我正在寻找表中方法调用的确切位置.优选地,通过示例证明它低于前一个并且高于下一个. 目前的答案似乎也没有提到带括号的方法调用. 提前致谢! 解决方法
序幕
这旨在测试所有可能的情况. 注意,当说“运算符X具有比方法调用更高的优先级”时,意味着在参数中.又名: invocation foo X bar 而不是(呼吁对象) X invocation 就第二种情况而言,方法调用始终具有更高的优先级. 简短的回答 它不适合: >在某些情况下会导致SyntaxError 摘要 >不管括号如何,在方法调用之后都不能使用 让我们尝试一下: class Noone < BasicObject undef_method :! def initialize(order) @order = order end def method_missing(name,*args) @order << name self end end 第一个一元: # + and - will become binary unary_operators = %i(! ~ not defined?) puts 'No brackets' unary_operators.each do |operator| puts operator order = [] foo = Noone.new order bar = Noone.new order begin eval("foo.meta #{operator} bar") rescue SyntaxError => e puts e end p order puts '-----------' end puts 'Brackets' unary_operators.each do |operator| puts operator order = [] foo = Noone.new order bar = Noone.new order begin eval("foo.meta(#{operator} bar)") rescue SyntaxError => e puts e end p order puts '-----------' end 积分: >不是在方法调用之后是SyntaxError 现在二进制: binary_operators = %i( ** * / % + - << >> & | ^ > >= < <= <=> == === =~ .. ... or and ) puts 'No brackets' binary_operators.each do |operator| order = [] foo = Noone.new order bar = Noone.new order baz = Noone.new order begin eval("foo.meta bar #{operator} baz") rescue SyntaxError => e puts e end p order end puts 'Brackets' binary_operators.each do |operator| order = [] foo = Noone.new order bar = Noone.new order baz = Noone.new order begin eval("foo.meta( bar #{operator} baz)") rescue SyntaxError => e puts e end p order end 积分: >方法调用周围的括号使用和或或是一个SyntaxError def yes puts 'yes' true end def no puts 'no' false end def anything(arg) puts 'Anything' arg end anything yes and no anything no or yes anything yes && no anything no || yes anything(yes && no) anything(no || yes) anything yes == no anything(yes == no) anything yes != no anything(yes != no) 积分: >和和或没有括号的优先级较低 def five(*args) p args 5 end five 2..7 five(2..7) five 2...7 five(2...7) 积分: > ..和…无论括号如何都具有更高的优先级 anything yes if no anything(yes if no) anything no unless yes anything(no unless yes) anything no until yes anything(no until yes) anything yes while no anything(yes while no) 积分: >括号if,until,while导致SyntaxError def error puts 'Error' raise end anything error rescue yes anything(error rescue yes) 积分: > rescue周围的括号会导致SyntaxError 三元: anything yes ? no : 42 anything(yes ? no : 42) 积分: >无论括号如何,三元都有更高的优先级 分配(留为最后,因为它改变是和否): anything yes = no anything(no = five(42)) 积分: >赋值具有比调用更高的优先级 请注意,=等只是和=的快捷方式,因此它们表现出相同的行为. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- c# – 具有服务层,业务层和实体框架的N层体系结构
- ruby-on-rails – Ruby on Rails捆绑降级
- cocos2d-x 3.8.1的骨骼动画加载方法addArmatureFileInfo仍然
- 替换C#中的第一个逗号
- Oracle SQL Developer unable to find Java Virtual Machin
- PostgreSQL学习笔记---如何包含有单引号的字符串
- Core Data 的简单使用
- Unable to connect to PostgreSQL server: FATAL: missing
- create-react-app项目配置
- Objective-C Library for RSS feed解析?