Access denied, Please sign in and make sure you have proper permission.
没有什么副作用。主要是可以让用户写出真正的方法链
0> "hello".methods.grep /str/
=> [:strip, :rstrip, :to_str, :lstrip, :strip!, :lstrip!, :rstrip!]
一般建议带参数的时候都带上括号这样代码更易读。在类中调用方法除外,在 Rails 里一般调用类方法都会采用类似于宏定义的写法
class Post < ApplicationRecord
belongs_to :category
validates :title, presence: true
end
其实写成这样也没差,只是没人会这么去写
class Post < ApplicationRecord
belongs_to(:category)
validates(:title, presence: true)
end
一开始觉得不写括号只是个语法糖,可有可无。
写的时间长了才发现,写括号跟不写括号,自己的心绪处在两个不同的世界,一个是数学的世界,一个是文学的世界
如果是嵌套的调用 比如 print(funcall(param)) 这种的只能拆成两行代码了吗?
不用啊。这样写都行print method :dup
只是可读性不好一般建议写成这样print method(:dup)
。