Err, Hi Sarah,
Well i'm a newbie to fresh out of the womb in fact but i think i can point you in the right direction. Firstly yes operators are implemented as methods so 1+2 would be interpreted as 1.+(2) and since they are methods there behavoiur for classes that you create can be specified by you. Operators do also have a precedence as operators do in C and are methods of all objects of the Numeric Class.
From what I understand 'div' and '/' and not exactly the same. So 1/2 would be seen as 1./(2) whereas if you used 'div', you would write 1.div(2). The operator '/' seems to be equipt to deal with spaces but not 'div', that is probaly why you would use '()' to pass the argument. You also have 'fdiv' for floating point division and 'quo' returns a rational where possible.
This exact topic if covered in glorious technicolour in 'The Ruby Programming Language' by Flanagan and Matsumoto, but I am sure you can google it also.
Imran Nazir
Friend, Boho, House Owner, Citizen, Engineer
···
________________________________
From: Sarah Allen <sarah@ultrasaurus.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Tue, 25 August, 2009 8:06:26
Subject: operators vs. methods
Today I was just re-reading introductory Ruby docs as I prepare to teach
a few newbies tomorrow and I find myself feeling newbie-ish.
On ruby-lang.org's Ruby from other languagage highlights
http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/
it points out that a method call is really a message to another object:
# This
1 + 2
# Is the same as this ...
1.+(2)
# Which is the same as this:
1.send "+", 2
That's all well and good, except that + isn't an ordinary method. If I
take another method like div:
# This
4.div(2)
# Is not the same as
4 div 2
In fact the latter is a syntax error.
When I first learned Ruby I was led to believe that + is just a method
with a funny name, but now that I think about it that cannot be. Not
only is there a special way that spaces are handled, but operator
precedence is pretty special too.
Where might I find this behavior documented? Is there a special list of
operators? How are they different from methods? Can I create my own?
Puzzled, or maybe I just need more sleep,
Sarah
--
Posted via http://www.ruby-forum.com/.