Ruby中的.length和.length()有什么区别?

我只是想知道是否.length()只是一个别名,.length因为它们返回相同的结果:

[38] pry(main)> temp = [1,2,3]
=> [1, 2, 3]
[40] pry(main)> temp.length
=> 3
[41] pry(main)> temp.length()
=> 3

回答

没有区别。在 Ruby 中,您可以在许多情况下省略参数周围的括号。

temp.length
temp.length() 

是相同的。同样适用于

temp.push(4)
temp.push 4

  • It's worth mentioning that there are cases where you want to use them, or need to, like in the case of `temp.push 4 * 5` or `temp.push(4) * 5` which mean two very different things.

以上是Ruby中的.length和.length()有什么区别?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>