Do operators in Ruby belong to a particular class ? As per my knowledge operators are tokens which are redefined by classes according to the operation they are intended to perform. For example the Numeric class defines the + operator for numeric operations, similarly the String class defines it for string concatenation. So based on that if i try to do this:
+.is_a ? (Numeric)
It returns false. Is my explanation to this question correct ?
Sorry for all the confusion this is the actual question from my assignment.What class does + belong to and how do you check it ?
Most of what is an operator in other programming languages, is just a method in Ruby.
+is a method not an operator. You can get reference to it:FixnumandStringare ones of many classes that implement+method. You can define your own operator-like methods:Examples of true operators in Ruby, you can’t define your own methods with some of these ‘names’:
=+=,-=,*=,/=,%=,**=&,|,^,~,<<,>>and,or,&&,||,!,not? :..,....,::There is also a special operator
defined?. It actually looks like a method but it’s an operator. You can define your owndefined?method, though.