+= operator overloading in ruby

前端 未结 1 444
有刺的猬
有刺的猬 2021-01-28 19:44
class Fracpri
attr_accessor:whole, :numer, :denom, :dec, :flofrac
def initialize()
    puts \"Hey! It\'s an empty constructor\"
end
def getFraction(whole,numer,denom)
           


        
相关标签:
1条回答
  • It is not possible to override =, nor variants such as +=. These are built in keywords and not methods such as +.

    If you change your patch from def +=(obj) to def +(obj), you can still call r1 += r2 and it will have the same effect as if you'd patched +=. This is because += is calling your patched + method under the hood.

    By the way, your + method doesn't actually return a value so any time you call += it will always result in nil .... but it seems like this is still a WIP so hopefully you can sort that bit out.

    0 讨论(0)
提交回复
热议问题