小编mpd*_*mpd的帖子

Ruby操作符方法调用与普通方法调用

我想知道为什么调用operator方法不需要点?或者更确切地说,为什么不能在没有点的情况下调用普通方法?

class Foo
  def +(object)
    puts "this will work"
  end
  def plus(object)
    puts "this won't"
  end
end 
f = Foo.new
f + "anything" # "this will work"
f plus "anything" # NoMethodError: undefined method `plus' for main:Object

ruby methods syntax

12
推荐指数
2
解决办法
3472
查看次数

标签 统计

methods ×1

ruby ×1

syntax ×1