how to call operator as function

Discussion in 'Ruby' started by Vladimir Fekete, Nov 15, 2008.

  1. 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.
     
    Vladimir Fekete, Nov 15, 2008
    #1
    1. Advertising

  2. Vladimir Fekete

    Peter Szinek Guest

    [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
     
    Peter Szinek, Nov 15, 2008
    #2
    1. Advertising

  3. Vladimir Fekete

    Stefan Lang Guest

    2008/11/15 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,


    Use send, if all operators are binary:

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

    etc.

    Stefan
     
    Stefan Lang, Nov 15, 2008
    #3
  4. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 15, 2008
    #4
  5. 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:
    > 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.
     
    Vladimir Fekete, Nov 15, 2008
    #5
  6. 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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 15, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. grbgooglefan
    Replies:
    2
    Views:
    432
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    447
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    399
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    255
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    195
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page