# Type specific operator

**URL:** <https://www.ruby-forum.com/t/type-specific-operator/142563>\
**Category:** Ruby\
**Created:** [July 9, 2008, 9:05am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563 "2008-07-09T09:05:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 9:05am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/1 "2008-07-09T09:05:12Z")

</div>

Hi all,  
Ruby is a dynamically typed language so it checks at run time the type  
of the variable and calls corresponding operator. For example “x + y”  
will be checked for type of x and y if both will be fixnum then  
corresponding addition action will be performed if both are strings the  
corresponding appending action will be performed.  
My question is"  
Is there any type specific operator facility available in ruby? If  
somehow we can infer the type of the variables then we can directly use  
those specific operators. I am not aware about any such operator. IF  
there is any please give me the references.

Thanks  
amitraj

---

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 9:14am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/2 "2008-07-09T09:14:06Z")

</div>

> Is there any type specific operator facility available in ruby?

You can overload operators on specific classes if you want custom  
behaviour:

class Foo  
attr\_reader :size

def initialize(n)  
@size = n  
end

def +(other)  
Foo.new(@size + other.size)  
end  
end

Foo.new(9) + Foo.new(3)

# =\> #\<Foo:0x7f8b6f762d00 @size=12\>

---

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 9:26am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/3 "2008-07-09T09:26:59Z")

</div>

On Wednesday 09 July 2008, Amitraj singh Chouhan wrote:

> there is any please give me the references.
> 
> Thanks  
> amitraj

I’m not sure I undestand correctly what you mean, but I think you have a  
wrong  
idea of how operators work in ruby. Most operators are simply shortcuts  
for  
method calls, so that, for example

1 + 3

is translated by the ruby interpreter to

1.+(3)

This means that, when ruby sees the expression 1 + 3, it tries to call  
the +  
method on the 1 object passing as argument the 3 object. If you write  
something like:

1 + ‘a’

ruby will try to pass ‘a’ as argument to the + method of 1. But since  
that  
method can only handle objects of class Numeric or objects which have a  
coerce  
method, and strings don’t have it, it raises an exception.

If you want to define operators for your own classes, you simply need to  
define the corresponding method:

class C

attr\_reader :x  
def initialize x  
@x = x  
end

def +(other)  
case other  
when Integer then return C.new other + @x  
when C then C.new @x + other.x  
else raise ArgumentError,“You can’t add an object of class  
#{other.class}”  
end  
end

end

c = C.new 3  
c1 = c + 2  
puts c1.x  
c2 = c + C.new(5)  
puts c2.x  
c + ‘a’

The above code gives:  
5  
8  
ArgumentError,"You can’t add an object of class String

(note that doing 1+c would raise a TypeError exception, since in this  
case the  
method call would be 1.+©, instead of c.+(1), and that Integer#+  
doesn’t  
know how to handle instances of class C).

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 10:28am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/4 "2008-07-09T10:28:19Z")

</div>

Thanks for making me correct. But as in case of x + y it gets converted  
to x.+(y). So here the type of x must be known(that is what I mean by  
checking type of x) and also to execute + method of x’s class and to  
pass it argument y, type of y must be known(that is what I mean by  
checking the type of y).  
These checkings will be done at run time. My question is if we have an  
Idea about the type of x and y initially(at the time of writing the  
program) can we eliminate these type checks. I mean is there any  
operator or method call that will directly treat x and y as fixnum. i.e.  
something like  
int+(x,y) //specific function for adding two  
integer  
string+(x,y) //specific function for appending  
two strings  
or any thing like that which will directly treat x and y as variables  
known types

I hope you will be getting my point.  
Please give me the details if there is any such thing in ruby.

Thanks  
amitraj

---

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 10:59am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/5 "2008-07-09T10:59:45Z")

</div>

On Wed, Jul 9, 2008 at 10:24 AM, Amitraj singh Chouhan  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for making me correct. But as in case of x + y it gets converted  
> to x.+(y). So here the type of x must be known(that is what I mean by  
> checking type of x) and also to execute + method of x’s class and to  
> pass it argument y, type of y must be known(that is what I mean by  
> checking the type of y).  
> You can call x.class but believe me that will not give you an exact  
> idea where the message :+ will be intercepted.

> These checkings will be done at run time.  
> I would not call it checkings, I would call it method dispatch. AFAIK  
> the message will be dispatched up the ancestor chain until an  
> implementing method is found, if none is found the same search is  
> repeated for #method\_missing, however…  
> My question is if we have an  
> Idea about the type of x and y initially(at the time of writing the  
> program) can we eliminate these type checks. I mean is there any  
> operator or method call that will directly treat x and y as fixnum. i.e.  
> something like  
> int+(x,y) //specific function for adding two  
> integer  
> string+(x,y) //specific function for appending  
> two strings  
> or any thing like that which will directly trea  
> I believe that people are working on such optimizations via dynamic  
> code analysis but really I am not good enough to give any detailed  
> information about this :(.

## Maybe you should have a closer look into jruby. HTH Robert

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

AALST (n.) One who changes his name to be further to the front  
D.Adams; The Meaning of LIFF

---

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 11:10am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/6 "2008-07-09T11:10:57Z")

</div>

Amitraj singh Chouhan wrote:

> These checkings will be done at run time. My question is if we have an  
> Idea about the type of x and y initially(at the time of writing the  
> program) can we eliminate these type checks.

Why would you want to? Are you trying to solve a specific problem, or  
understand conceptually? Are you assuming some sort of efficiency gain  
by doing as you would like?

> I mean is there any  
> operator or method call that will directly treat x and y as fixnum. i.e.  
> something like  
> int+(x,y) //specific function for adding two  
> integer  
> string+(x,y) //specific function for appending  
> two strings  
> or any thing like that which will directly treat x and y as variables  
> known types  
> Objects such as Fixnum and String have a + method that they respond to:
> 
> > x=1  
> > =\> 1
> 
> > x.respond\_to?("+")  
> > =\> true  
> > or not:
> 
> > h= {:a =\>1}  
> > =\> {:a=\>1}
> 
> > h2 = {:b =\> “foo”}  
> > =\> {:b=\>“foo”}
> 
> > h.respond\_to?("+")  
> > =\> false
> 
> > new\_h = h+h2  
> > NoMethodError: undefined method `+’ for {:a=\>1}:Hash  
> > from (irb):3

This is the idea behind DuckTyping. It doesn’t matter the type of the  
object, so long as it responds to a specific message, all is well. As  
you see above, you can investigate for yourself ahead of time if the  
receiver responds to a particular message with respond\_to?, but that  
doesn’t really accomplish what you are looking for, specifically, in  
terms of the Ruby interpreter doing it’s job at runtime.

Howard

---

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/7 "2008-07-09T14:43:34Z")

</div>

Hajime-mashite \> Amitraj-san

I’m not sure I got your point of problem, but  
the discussion so far reminds me of a conceptual problem;

in Smalltalk(and in ruby, nowadays),  
(1 + ‘a’) and (‘a’ + 1) could give different answers.  
These language isn’t ‘symmetrical’ in such point.

So…

On 2008/07/09, at 18:51, Amitraj singh Chouhan wrote:

> not any specific program but any program which uses such operators.

if it’s your problem for daily life,  
You could give a try on CLOS: a OOL in Lisp fashion.

Or CLOS is not practical for you,  
You could define Classes in Ruby to  
establish Sets --in mathematical sense-- in which  
your Operators of concern go with Commutability.

# Ruby is in a spirits of Do-It-YourSelf, I guess.

oop. moderate choice is there: Smalltalk.

The choices are up to you.

=======================  
2 bits thinking helps 66.7%

Shindo Motoaki (Motoakira)

# 21, Higashi-Kurayoshi machi Yonago City, Tottori Pref. 683-0815, Japan TEL 81-859-33-7431/81-90-7593-3585 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Amitraj\_singh\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/53a042/32.png) [@Amitraj\_singh\_C](https://www.ruby-forum.com/u/Amitraj_singh_C)\
**Post date:** [July 9, 2008, 11:55am UTC](https://www.ruby-forum.com/t/type-specific-operator/142563/8 "2008-07-09T11:55:42Z")

</div>

> Why would you want to? Are you trying to solve a specific problem, or  
> understand conceptually? Are you assuming some sort of efficiency gain  
> by doing as you would like?  
> Yes I would like to optimize the program, not any specific program but  
> any program which uses such operators.

Can I get some reference for the working of the interpreter, I mean how  
it process the given ruby program. Is there any link or document which  
describes the step by step actions performed by interpreter and the  
description of those steps.

-amitraj
