# Error while overloading the '/' operator

**URL:** <https://www.ruby-forum.com/t/error-while-overloading-the-operator/179935>\
**Category:** Rails\
**Created:** [December 8, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/error-while-overloading-the-operator/179935 "2009-12-08T12:23:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lune9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e79b87/32.png) [@lune9](https://www.ruby-forum.com/u/lune9)\
**Post date:** [December 8, 2009, 12:23pm UTC](https://www.ruby-forum.com/t/error-while-overloading-the-operator/179935/1 "2009-12-08T12:23:15Z")

</div>

Hi,

I tried to overload the operator ‘/’ with float figures :

class Float

def /(dividande)  
if (self.to\_f == 0.0 or dividande.to\_f == 0.0)  
return 0.0  
else  
return self.div(dividande)  
end  
end

end

But when I use it I’ve got this error :

./common/tools.rb:7:in `/': stack level too deep (SystemStackError) from ./common/tools.rb:7:in`div’

so this line “return self.div(dividande)”

Does anyone can help me ?

Thanks

---

<div class="post-metadata">

**Author:** ![lune9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e79b87/32.png) [@lune9](https://www.ruby-forum.com/u/lune9)\
**Post date:** [December 8, 2009, 2:57pm UTC](https://www.ruby-forum.com/t/error-while-overloading-the-operator/179935/2 "2009-12-08T14:57:41Z")

</div>

Lune L. wrote:

> Hi,
> 
> I tried to overload the operator ‘/’ with float figures :
> 
> class Float
> 
> def /(dividande)  
> if (self.to\_f == 0.0 or dividande.to\_f == 0.0)  
> return 0.0  
> else  
> return self.div(dividande)  
> end  
> end
> 
> end
> 
> But when I use it I’ve got this error :
> 
> ./common/tools.rb:7:in `/': stack level too deep (SystemStackError) from ./common/tools.rb:7:in`div’
> 
> so this line “return self.div(dividande)”
> 
> Does anyone can help me ?
> 
> Thanks

Have you considered the following possibility?  
using your code… I try to do this:

> > 5.5 / 7  
> > The program says "Okay, 5.5 isn’t 0 and 7 isn’t 0, so I’m going to …  
> > Go to this “divide” function there, which tells me to check whether 5.5  
> > is 0 and 7 is 0, and otherwise I have to go … to this “divide”  
> > function there, which tells me to check …

What you’re really looking for is a way to call the old “divide” method.  
The usual way of doing it is to first alias the original method to  
another name (something like  
“boo\_i\_hate\_the\_original\_dividing\_for\_floats”), and then call that  
method in your code if it is warranted.

---

<div class="post-metadata">

**Author:** ![lune9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e79b87/32.png) [@lune9](https://www.ruby-forum.com/u/lune9)\
**Post date:** [December 10, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/error-while-overloading-the-operator/179935/3 "2009-12-10T17:11:21Z")

</div>

Robert W. wrote:

> Sorry, this is somewhat picky, but this is not method “overloading.”  
> This is method “overriding” since you are replacing the existing method  
> with your own. Method overloading is something completely different.  
> Ruby does not directly support method overloading.

Hum. Ignore this. I misread the OP a little by thinking “method  
overloading” as opposed to “operator overloading.” Although, in Ruby  
this can often be somewhat of a gray area, given that Ruby operators  
basically are methods. It does confuse terminology somewhat.

---

<div class="post-metadata">

**Author:** ![lune9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e79b87/32.png) [@lune9](https://www.ruby-forum.com/u/lune9)\
**Post date:** [December 10, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/error-while-overloading-the-operator/179935/4 "2009-12-10T16:55:41Z")

</div>

Lune L. wrote:

> I tried to overload the operator ‘/’ with float figures :
> 
> class Float
> 
> def /(dividande)  
> if (self.to\_f == 0.0 or dividande.to\_f == 0.0)  
> return 0.0  
> else  
> return self.div(dividande)  
> end  
> end
> 
> end

Sorry, this is somewhat picky, but this is not method “overloading.”  
This is method “overriding” since you are replacing the existing method  
with your own. Method overloading is something completely different.  
Ruby does not directly support method overloading.

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/234117](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/234117)
