# How to call operator as function

**URL:** <https://rubytalk.org/t/how-to-call-operator-as-function/50187>\
**Category:** ruby-talk\
**Created:** [15 November 2008 10:56 UTC](https://rubytalk.org/t/how-to-call-operator-as-function/50187 "2008-11-15T10:56:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vladimir\_Fekete](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@Vladimir\_Fekete](https://rubytalk.org/u/Vladimir_Fekete)\
**Post date:** [15 November 2008 10:56 UTC](https://rubytalk.org/t/how-to-call-operator-as-function/50187/1 "2008-11-15T10:56:19Z")

</div>

Hi \*,

could you help me please ? I'm trying to write a code where I could have  
operators' functions as values of hash and executing them in way like this  
(formal code):

> **···**
>
> #=============================  
> operators = { "+" =\> Fixnum.+,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-" =\> Fixnum.-,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"++" =\> Myclass.++}
> 
> op = "+"
> 
> result = operators[op](left,right) if left.class==Fixnum and right.class==Fixnum  
> #============================
> 
> is it possible ? Thanks,
> 
> Cheers,
> 
> V.

---

<div class="post-metadata">

**Author:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [15 November 2008 11:05 UTC](https://rubytalk.org/t/how-to-call-operator-as-function/50187/2 "2008-11-15T11:05:49Z")

</div>

++ is not supported by Ruby 🙂

I would do

1.send '+'.to\_sym, 2

unless you want to use some esoteric operator mappings, you don't need the hash.  
Fixnum.+ doesn't make that much sense because of Ruby's duck typing (and why would you want to restrict yourself to work with just certain objects?)

p.s.: dobre tu vidiet matfyzaka 🙂

Cheers,  
Peter

> **···**
>
> On 2008.11.15., at 11:56, Vladimir Fekete wrote:
> 
> > Hi \*,
> > 
> > could you help me please ? I'm trying to write a code where I could have  
> > operators' functions as values of hash and executing them in way like this  
> > (formal code):
> > 
> > #=============================  
> > operators = { "+" =\> Fixnum.+,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-" =\> Fixnum.-,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"++" =\> Myclass.++}
> > 
> > op = "+"
> > 
> > result = operators[op](left,right) if left.class==Fixnum and right.class==Fixnum  
> > #============================
> 
> \_\_\_  
> [http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![Stefan\_Lang1](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@Stefan\_Lang1](https://rubytalk.org/u/Stefan_Lang1)\
**Post date:** [15 November 2008 11:06 UTC](https://rubytalk.org/t/how-to-call-operator-as-function/50187/3 "2008-11-15T11:06:50Z")

</div>

Use send, if all operators are binary:

&nbsp;&nbsp;left.send(:+, right)  
&nbsp;&nbsp;left.send(:-, right)

etc.

Stefan

> **···**
>
> 2008/11/15 Vladimir Fekete \<fekete@melkor.dnp.fmph.uniba.sk\>:
> 
> > Hi \*,
> > 
> > could you help me please ? I'm trying to write a code where I could have  
> > operators' functions as values of hash and executing them in way like this  
> > (formal code):
> > 
> > #=============================  
> > operators = { "+" =\> Fixnum.+,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-" =\> Fixnum.-,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"++" =\> Myclass.++}
> > 
> > op = "+"
> > 
> > result = operators[op](left,right) if left.class==Fixnum and right.class==Fixnum  
> > #============================
> > 
> > is it possible ? Thanks,

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [15 November 2008 11:07 UTC](https://rubytalk.org/t/how-to-call-operator-as-function/50187/4 "2008-11-15T11:07:17Z")

</div>

Vladimir Fekete wrote:

> operators = { "+" =\> Fixnum.+,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"-" =\> Fixnum.-,

"+" =\> Fixnum.instance\_method(:+),  
"-" =\> Fixnum.instance\_method(:-),

> ```
> &quot;\+\+&quot; =&gt; Myclass\.\+\+\}
> 
> ```

There is no ++ operator in ruby and you can't define one.

> op = "+"
> 
> result = operators[op](left,right) if left.class==Fixnum and  
> right.class==Fixnum

result = operators[op].bind(left).call(right) if left.is\_a?(Fixnum) and  
right.is\_a?(Fixnum)

Or you skip the whole thing with the hash and just do:

op = "+"  
result = left.send(op, right)

HTH,  
Sebastian

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Vladimir\_Fekete](https://avatars.discourse-cdn.com/v4/letter/v/a4c791/32.png) [@Vladimir\_Fekete](https://rubytalk.org/u/Vladimir_Fekete)\
**Post date:** [15 November 2008 11:15 UTC](https://rubytalk.org/t/how-to-call-operator-as-function/50187/5 "2008-11-15T11:15:42Z")

</div>

> Vladimir Fekete wrote:  
> \> operators = { "+" =\> Fixnum.+,  
> \> "-" =\> Fixnum.-,
> 
> "+" =\> Fixnum.instance\_method(:+),  
> "-" =\> Fixnum.instance\_method(:-),
> 
> \> ? ? ? ? ? ? ? "++" =\> Myclass.++}
> 
> There is no ++ operator in ruby and you can't define one.

it's Myclasses ++ operator

> \> op = "+"  
> \>  
> \> result = operators[op](left,right) ?if left.class==Fixnum and  
> \> right.class==Fixnum
> 
> result = operators[op].bind(left).call(right) if left.is\_a?(Fixnum) and  
> right.is\_a?(Fixnum)
> 
> Or you skip the whole thing with the hash and just do:
> 
> op = "+"  
> result = left.send(op, right)
> 
> HTH,  
> Sebastian  
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

What I'm trying to do is Domain specific language class, for which I could set  
up names of variables, functions and operators and then parse and execute  
a string (f.e. "( a + ( b \* c ) + sin ( d ) )", where  
variables could be hash { "a" =\> 1, "b" =\> 2, "c" =\> 3, "d" =\> 4} and  
functions could be hash again so naturaly I asked whether it is possible to  
make it with operators). I tryied to use som ruby's or irb's native  
DSL but withouth success.

V.

> **···**
>
> On Sat, Nov 15, 2008 at 08:07:17PM +0900, Sebastian Hungerecker wrote:

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [15 November 2008 11:20 UTC](https://rubytalk.org/t/how-to-call-operator-as-function/50187/6 "2008-11-15T11:20:38Z")

</div>

Vladimir Fekete wrote:

> it's Myclasses ++ operator

You can't define ++ on any class. You just can't. If the parser sees ++  
anywhere it interprets it as two calls to +@ or one call to + and one to +@.

HTH,  
Sebastian

> **···**
>
> --  
> NP: Dire Straits - Your Latest Trick  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826
