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: