在Ruby中,为什么在“执行”时方法调用不能被视为单元。和“结束”用来? [英] In Ruby, why is a method invocation not able to be treated as a unit when "do" and "end" is used?

查看:104
本文介绍了在Ruby中,为什么在“执行”时方法调用不能被视为单元。和“结束”用来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题与 Ruby打印注入语法问题相关。我的问题是,我们可以坚持使用 do end 并使它与 puts一起工作或 p



这样做:

  a = [1,2,3,4] 

b = a.inject do | sum,x |
sum + x
end
puts b#打印出10

因此,说 inject 是Array对象的一个​​实例方法是正确的,并且此实例方法接收一段代码,然后返回一个数字。如果是这样,那么它应该与调用函数或方法并获取返回值没有区别: 3)
把b



  b = circle.getRadius()
puts b

在以上两种情况下,我们可以直接说出:

  puts foo(3)
放圆圈。 getRadius()

所以,没有办法通过使用以下两种方法直接使它工作:

  a = [1,2,3,4] 

放入a.inject do | sum ,x |
sum + x
end

但它给了

  ch01q2.rb:7:在`inject'中:没有给出块(LocalJumpError)
来自ch01q2.rb:4: ch01q2.rb中的
:4:在ch01q2.rb中注入
:4

使用()对方法调用进行分组也不起作用:

  a = [1,2,3,4] 

puts(a.inject do | sum,x |
sum + x
end)

这就给出了:

  ch01q3.rb:4:语法错误,意外的kDO_BLOCK,期待')'
puts(a.inject do | sum,x |
^
ch01q3.rb:4:语法错误,意外的'|',期待'='
puts(a.inject do | sum,x |
^
ch01q3.rb:6:语法错误,意想不到的kEND,期待$结束
结束)
^

最后,以下版本起作用:

  a = [1,2,3,4] 

把a.inject {| sum ,x |
sum + x
}

但为什么不分组方法调用使用()在前面的例子中工作?如果程序员坚持他使用 do end ,那么它可以工作吗?

$ (非官方)

/ a>,我们发现 puts(...)中的(...)的内容必须是 CALL_ARGS ,它不会直接减少到 STMT 。但是,他们可以减少到'('COMPSTMT')'。通过包含一组额外的括号,您可以使用 do ... end

  a = [1,2,3,4] 

puts((a.inject do | sum,x |
sum + x
end))


The following question is related to the question "Ruby Print Inject Do Syntax". My question is, can we insist on using do and end and make it work with puts or p?

This works:

a = [1,2,3,4]

b = a.inject do |sum, x|
  sum + x
end
puts b   # prints out 10

so, is it correct to say, inject is an instance method of the Array object, and this instance method takes a block of code, and then returns a number. If so, then it should be no different from calling a function or method and getting back a return value:

b = foo(3)
puts b

or

b = circle.getRadius()
puts b

In the above two cases, we can directly say

puts foo(3)
puts circle.getRadius()

so, there is no way to make it work directly by using the following 2 ways:

a = [1,2,3,4]

puts a.inject do |sum, x|
  sum + x
end

but it gives

ch01q2.rb:7:in `inject': no block given (LocalJumpError)
        from ch01q2.rb:4:in `each'
        from ch01q2.rb:4:in `inject'
        from ch01q2.rb:4

grouping the method call using ( ) doesn't work either:

a = [1,2,3,4]

puts (a.inject do |sum, x| 
        sum + x   
      end)

and this gives:

ch01q3.rb:4: syntax error, unexpected kDO_BLOCK, expecting ')'
puts (a.inject do |sum, x|
                 ^
ch01q3.rb:4: syntax error, unexpected '|', expecting '='
puts (a.inject do |sum, x|
                          ^
ch01q3.rb:6: syntax error, unexpected kEND, expecting $end
      end)
         ^

finally, the following version works:

a = [1,2,3,4]

puts a.inject { |sum, x|
    sum + x
}

but why doesn't the grouping of the method invocation using ( ) work in the earlier example? What if a programmer insist that he uses do and end, can it be made to work?

解决方案

From the (unofficial) ruby grammar, we see that the contents of (...) in puts (...) must be CALL_ARGS, which don't directly reduce to STMT. However, they can reduce to '(' COMPSTMT ')'. By including an extra set of parentheses, you can use do ... end.

a = [1,2,3,4]

puts ((a.inject do |sum, x| 
         sum + x   
       end))

这篇关于在Ruby中,为什么在“执行”时方法调用不能被视为单元。和“结束”用来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆