Operator overloading

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Operator overloading

    Hi,

    With Ruby operaotr overloading is simply done by a definition like

    class foo
    ...

    def +( other )
    # some code
    end

    ...
    end

    I thought I did something very clever by defining also:

    class foo
    ...

    def +=( other )
    # some code
    end

    ...
    end

    but Ruby wasn't impressed and gave me a SYNTAX ERROR instead on the
    first opening "(".

    Is it possible to overload operators like += -= &&= and such ?

    And how can I do this ?

    Kind regards and have a nice weekend,
    Meino

    Meino Christian Cramer Guest

  2. Similar Questions and Discussions

    1. Overloading WebMethods
      I have webservice and I need to overload a method but when overloading the method as following i got error message written below: public int...
    2. Is Overloading supported?
      Hi all is overloading supported in a WebService procedure Because, since when I use it, I receive an error on client-side about the procedure...
    3. Overloading ()
      Hi, I was reading the comp.lang.functional group, and happened across a little discussion of Ruby vs. Python on there. One thing the Python guy...
    4. [PHP] OO function overloading?
      Hi, This statement isn't entirely correct, overloading is possible with the overload extension. http://www.php.net/overload Regards, Greg...
    5. method overloading?
      Is there a way to implement method overloading in ruby? Naively I just defined two methods with the same name with different number of...
  3. #2

    Default Re: Operator overloading

    >>>>> "M" == Meino Christian Cramer <Meino.Cramer@gmx.de> writes:

    M> Is it possible to overload operators like += -= &&= and such ?

    += is not an operator.

    a += 1

    is just a shortcut for

    a = a + 1

    Guy Decoux



    ts Guest

  4. #3

    Default Re: Operator overloading

    Hi,

    I'm pretty new to Ruby, so someone may step in quickly to correct me. But
    my understanding is that, for any of the standard dyadic operators (+, -,
    etc.) the shorthand

    j += 1

    is expanded before evaluation to:

    j = j + 1

    I've experimented with a couple of overloads of the '+' operator, and I
    always got exactly what I expected from '+=' for free...Principle of Least
    Surprise, indeed :-)

    - dan


    "Meino Christian Cramer" <Meino.Cramer@gmx.de> wrote in message
    news:20030921.143434.92569875.Meino.Cramer@gmx.de. ..

    > Is it possible to overload operators like += -= &&= and such ?
    >
    > And how can I do this ?
    >
    > Kind regards and have a nice weekend,
    > Meino
    >

    dhtapp Guest

  5. #4

    Default Re: Operator overloading

    On Sun, 21 Sep 2003 11:29:27 -0700, dhtapp wrote:
    > Hi,
    >
    > I'm pretty new to Ruby, so someone may step in quickly to correct me. But
    > my understanding is that, for any of the standard dyadic operators (+, -,
    > etc.) the shorthand
    >
    > j += 1
    >
    > is expanded before evaluation to:
    >
    > j = j + 1
    Not quite - I think j += 1 only evaluates j once. I recall one instance
    where that helped me, performance-wise.

    --
    Tom Felker, <tcfelker@mtco.com>
    <http://vlevel.sourceforge.net> - Stop fiddling with the volume knob.

    How long do we want to live in a world where profit is made by forcing the
    competition to reinvent the wheel?

    Tom Felker Guest

  6. #5

    Default Re: Operator overloading

    Hi --

    On Mon, 22 Sep 2003, Tom Felker wrote:
    > On Sun, 21 Sep 2003 11:29:27 -0700, dhtapp wrote:
    >
    > > Hi,
    > >
    > > I'm pretty new to Ruby, so someone may step in quickly to correct me. But
    > > my understanding is that, for any of the standard dyadic operators (+, -,
    > > etc.) the shorthand
    > >
    > > j += 1
    > >
    > > is expanded before evaluation to:
    > >
    > > j = j + 1
    >
    > Not quite - I think j += 1 only evaluates j once. I recall one instance
    > where that helped me, performance-wise.
    The j on the left isn't being evaluated; it's just being assigned to.
    So in cases with objects that can have more than one instance (like
    String), you get a new object:

    irb(main):001:0> j = "abc"
    => "abc"
    irb(main):002:0> j.id
    => 537889088
    irb(main):003:0> j += "d"
    => "abcd"
    irb(main):004:0> j.id
    => 537881918

    With integers you don't, of course, but it's also a case of assigning
    to a variable, where the variable name on the left just serves to
    accept the assignment (in the expanded form).


    David

    --
    David Alan Black
    home: [email]dblack@superlink.net[/email]
    work: [email]blackdav@shu.edu[/email]
    Web: [url]http://pirate.shu.edu/~blackdav[/url]


    dblack@superlink.net Guest

  7. #6

    Default Re: Operator overloading


    "Meino Christian Cramer" <Meino.Cramer@gmx.de> schrieb im Newsbeitrag
    news:20030921.143434.92569875.Meino.Cramer@gmx.de. ..
    > Is it possible to overload operators like += -= &&= and such ?
    >
    > And how can I do this ?
    You can't. And you don't need to. As others pointed out already, Ruby
    treats all the assignment arithmetic operators as shortcuts and invokes
    the appropriate operator. So

    foo (op)= bar <=> foo = foo (op) bar

    Where (op) is pretty much every operator you'd like to put there.
    Implementing Foo#+ gives you Foo+= automatically.

    Regards

    robert

    Robert Klemme Guest

  8. #7

    Default Re: Operator overloading

    > > > j += 1
    > > >
    > > > is expanded before evaluation to:
    > > >
    > > > j = j + 1
    > >
    > > Not quite - I think j += 1 only evaluates j once. I recall one instance
    > > where that helped me, performance-wise.
    >
    > The j on the left isn't being evaluated; it's just being assigned to.
    > So in cases with objects that can have more than one instance (like
    > String), you get a new object:
    He is referring to this case:

    def counter
    c=0
    return proc { c+=1 }
    end

    cc = counter

    a = [ "a", "b", "c" ]
    a[cc.call] += "x" # only one cc.call evaluated, only a[1] changed
    a # [ "a", "bx", "c" ]

    cc = counter
    a = [ "a", "b", "c" ]
    a[cc.call] = a[cc.call] + "x" # a[1] = a[2] + "x"
    a # [ "a", "cx", "c" ]



    Carlos Guest

  9. #8

    Default Re: Operator overloading

    "Robert Klemme" <bob.news@gmx.net> wrote in message news:<bkmq5r$3dhd5$2@ID-52924.news.uni-berlin.de>...
    > You can't. And you don't need to. As others pointed out already, Ruby
    > treats all the assignment arithmetic operators as shortcuts and invokes
    > the appropriate operator. So
    Er. "You don't need to" is rather extreme. What about numerical cases?

    i.e., if m is a large matrix, then "m += 1" and "m = m + 1" are rather
    different beasts, since the "obvioius" interpretation of the first involves
    simply incrementing the matrix in place, while the second involves a
    copy.

    Is there a standard work-around for this? This was one of the reasons I
    stuck with python over ruby, but I've got to admit that I didn't spend a
    huge amount of time researching.

    Thanks for any info,

    -Johann
    Johann Hibschman Guest

  10. #9

    Default Re: Operator overloading


    --JI+G0+mN8WmwPnOn
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Johann Hibschman (jhibschman@yahoo.com) wrote:
    > i.e., if m is a large matrix, then "m +=3D 1" and "m =3D m + 1" are rather
    > different beasts, since the "obvioius" interpretation of the first involv=
    es
    > simply incrementing the matrix in place, while the second involves a
    > copy.
    Really? I would say that the second means the same as the first,
    because that's the way it works everywhere else in Ruby.

    Maybe Matz has programmed my brain for the first to be the same as
    the second.

    --=20
    Eric Hodel - [email]drbrain@segment7.net[/email] - [url]http://segment7.net[/url]
    All messages signed with fingerprint:
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


    --JI+G0+mN8WmwPnOn
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (FreeBSD)

    iD8DBQE/b2ESMypVHHlsnwQRArTDAKCGpPU9NYMOrHhZFjDmkp35KuQCkw CfVA8c
    3a4TVqXbXhMkjhB9el/G3GI=
    =ZCX1
    -----END PGP SIGNATURE-----

    --JI+G0+mN8WmwPnOn--

    Eric Hodel Guest

  11. #10

    Default Re: Operator overloading

    Johann Hibschman wrote:
    > "Robert Klemme" <bob.news@gmx.net> wrote in message news:<bkmq5r$3dhd5$2@ID-52924.news.uni-berlin.de>...
    >
    >
    >>You can't. And you don't need to. As others pointed out already, Ruby
    >>treats all the assignment arithmetic operators as shortcuts and invokes
    >>the appropriate operator. So
    >
    >
    > Er. "You don't need to" is rather extreme. What about numerical cases?
    >
    > i.e., if m is a large matrix, then "m += 1" and "m = m + 1" are rather
    > different beasts, since the "obvioius" interpretation of the first involves
    > simply incrementing the matrix in place, while the second involves a
    > copy.
    >
    > Is there a standard work-around for this? This was one of the reasons I
    > stuck with python over ruby, but I've got to admit that I didn't spend a
    > huge amount of time researching.
    narray has

    self.add! other
    self.sbt! other
    self.mul! other
    self.div! other
    self.mod! other

    which presumably are in-place ops. Anyway, they are faster than *= and
    friends:

    user system total real
    warmup 16.980000 2.850000 19.830000 ( 22.501646)
    using *= and /= 0.400000 0.050000 0.450000 ( 0.462865)
    using mul! and div! 0.160000 0.020000 0.180000 ( 0.181544)
    using no ops 0.000000 0.000000 0.000000 ( 0.006473)

    (on PIII850).

    =================================
    require 'narray'
    require 'benchmark'

    Benchmark.bm(22) do |test|

    reps = 10_000

    m = NMatrix.float(100,100).fill(1)

    test.report("warmup") do
    (1..reps).each do |i|
    m *= i
    m /= i
    end
    end

    m = NMatrix.float(3,3).fill(1)

    test.report("using *= and /=") do
    (1..reps).each do |i|
    m *= i
    m /= i
    end
    end

    m = NMatrix.float(3,3).fill(1)

    test.report("using mul! and div!") do
    (1..reps).each do |i|
    m.mul! i
    m.div! i
    end
    end

    m = NMatrix.float(3,3).fill(1)

    test.report("using no ops") do
    (1..reps).each do |i|
    end
    end

    end


    Joel VanderWerf Guest

  12. #11

    Default Re: Operator overloading

    Joel VanderWerf wrote:
    > user system total real
    > warmup 16.980000 2.850000 19.830000 ( 22.501646)
    > using *= and /= 0.400000 0.050000 0.450000 ( 0.462865)
    > using mul! and div! 0.160000 0.020000 0.180000 ( 0.181544)
    > using no ops 0.000000 0.000000 0.000000 ( 0.006473)
    Oops. I meant for all arrays to be 100x100, in which case the times are
    more reasonable:

    user system total real
    warmup 16.900000 2.740000 19.640000 ( 22.567519)
    using *= and /= 16.650000 2.820000 19.470000 ( 22.195233)
    using mul! and div! 5.360000 0.440000 5.800000 ( 6.051514)
    using no ops 0.010000 0.000000 0.010000 ( 0.006231)

    But there is still a factor of 3 improvement in mul! and div! over *=
    and /=.


    Joel VanderWerf Guest

  13. #12

    Default Re: Operator overloading


    "Johann Hibschman" <jhibschman@yahoo.com> schrieb im Newsbeitrag
    news:41b772c.0309221105.26d32b4f@posting.google.co m...
    > "Robert Klemme" <bob.news@gmx.net> wrote in message
    news:<bkmq5r$3dhd5$2@ID-52924.news.uni-berlin.de>...
    >
    > > You can't. And you don't need to. As others pointed out already,
    Ruby
    > > treats all the assignment arithmetic operators as shortcuts and
    invokes
    > > the appropriate operator. So
    >
    > Er. "You don't need to" is rather extreme. What about numerical cases?
    >
    > i.e., if m is a large matrix, then "m += 1" and "m = m + 1" are rather
    > different beasts, since the "obvioius" interpretation of the first
    involves
    > simply incrementing the matrix in place, while the second involves a
    > copy.
    >
    > Is there a standard work-around for this?
    Resort to methods with proper names (like "add!", "mult!" etc. Convention
    is, that an exclamation mark denotes methods that modify the receiver).

    Personally I feel very comfortable with Ruby not encouraging overloading
    operators all the time as C++ does. Readability doesn't always benefit
    from overloading. And there is nothing that can't be achieved with a
    method call (from a functional perspective).

    Regards

    robert

    Robert Klemme Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139