how to call operator as function

V

Vladimir Fekete

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.
 
P

Peter Szinek

[Note: parts of this message were removed to make it a legal post.]


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
 
S

Stefan Lang

2008/11/15 Vladimir Fekete said:
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
 
S

Sebastian Hungerecker

Vladimir said:
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: (e-mail address removed)
ICQ: 205544826
 
V

Vladimir Fekete

"+" => Fixnum.instance_method:)+),
"-" => Fixnum.instance_method:)-),


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

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.
 
S

Sebastian Hungerecker

Vladimir said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top