在理解ruby的過程中,我試圖用默認參數值覆蓋'+'。像這樣的東西。覆蓋'+'方法
class C
def something(a = 5)
puts "Received: #{a}"
end
def +(b = 10)
puts "Received: #{b}"
end
end
現在
x = C.new
x.something #=> Received: 5
x.something(88) #=> Received: 88
x.+ #=> IRB shows ? whereas I was expecting an output 'Received: 10'
這是因爲運算符優先級?
'x.send(:+)'會顯示你的期望。我相信解釋者在+之後會期待一種方法或變量。 – Gazler 2013-02-27 14:17:44
Gazler,太棒了。 send()按預期工作。現在我瞭解了send的實際用法。 – Bala 2013-02-27 14:24:56
@Bala發送所有方法調度... – texasbruce 2013-02-27 14:37:53