Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to call operator as function

Reply
Thread Tools

how to call operator as function

 
 
Vladimir Fekete
Guest
Posts: n/a
 
      11-15-2008
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.+,
"-" => Fixnum.-,
"++" => Myclass.++}

op = "+"

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

is it possible ? Thanks,

Cheers,

V.

 
Reply With Quote
 
 
 
 
Peter Szinek
Guest
Posts: n/a
 
      11-15-2008
[Note: parts of this message were removed to make it a legal post.]


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.+,
> "-" => Fixnum.-,
> "++" => Myclass.++}
>
> op = "+"
>
> result = operators[op](left,right) if left.class==Fixnum and
> right.class==Fixnum
> #============================


++ 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
___
http://www.rubyrailways.com
 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      11-15-2008
2008/11/15 Vladimir Fekete <(E-Mail Removed)>:
> 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.+,
> "-" => Fixnum.-,
> "++" => Myclass.++}
>
> op = "+"
>
> result = operators[op](left,right) if left.class==Fixnum and right.class==Fixnum
> #============================
>
> is it possible ? Thanks,


Use send, if all operators are binary:

left.send(:+, right)
left.send(:-, right)

etc.

Stefan

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      11-15-2008
Vladimir Fekete wrote:
> operators =3D { "+" =3D> Fixnum.+,
> "-" =3D> Fixnum.-,


"+" =3D> Fixnum.instance_method(:+),
"-" =3D> Fixnum.instance_method(,

> =A0 =A0 =A0 =A0 =A0 =A0 =A0 "++" =3D> Myclass.++}


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


> op =3D "+"
>
> result =3D operators[op](left,right) =A0if left.class=3D=3DFixnum and
> right.class=3D=3DFixnum


result =3D operators[op].bind(left).call(right) if left.is_a?(Fixnum) and=20
right.is_a?(Fixnum)


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

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


HTH,
Sebastian
=2D-=20
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Vladimir Fekete
Guest
Posts: n/a
 
      11-15-2008


On Sat, Nov 15, 2008 at 08:07:17PM +0900, Sebastian Hungerecker wrote:
> 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: (E-Mail Removed)
> 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.

 
Reply With Quote
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      11-15-2008
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: (E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
function Object,function statement,function operator alex Javascript 3 12-28-2006 02:57 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments