Ruby围绕方法定义中的参数括起来

我知道许多 ruby样式指南都坚持围绕方法定义的方法参数括起来.我理解有时在语法上需要括号来进行方法调用.

但是,任何人都可以提供为什么Ruby需要围绕方法定义的参数括号的示例吗?基本上,我正在寻找除“它看起来更好”之外的原因.

解决方法

如果你既没有括号也没有分号 as pointed out in this comment,那就不明确了.
def foo a end # => Error because it is amgiguous.
# Is this a method `foo` that takes no argument with the body `a`,or
# Is this a method `foo` that takes an argument `a`?

另外,当您想使用复合参数时,不能省略括号:

def foo(a,b),c
  p [a,b,c]
end
# => Error

def foo((a,c)
  p [a,c]
end
foo([1,2],3) # => [1,2,3]

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...