[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Operator overloading

Meino Christian Cramer

9/21/2003 12:33:00 PM

9 Answers

ts

9/21/2003 12:40:00 PM

0

>>>>> "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



dhtapp

9/21/2003 6:29:00 PM

0

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
>


Tom Felker

9/22/2003 5:56:00 AM

0

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.sourcefor... - 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?

Robert Klemme

9/22/2003 12:19:00 PM

0


"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

jhibschman

9/22/2003 7:05:00 PM

0

"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

Eric Hodel

9/22/2003 8:53:00 PM

0

Johann Hibschman (jhibschman@yahoo.com) wrote:

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

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.

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

Joel VanderWerf

9/22/2003 9:15:00 PM

0

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

9/22/2003 9:24:00 PM

0

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 /=.


Robert Klemme

9/23/2003 6:54:00 AM

0


"Johann Hibschman" <jhibschman@yahoo.com> schrieb im Newsbeitrag
news:41b772c.0309221105.26d32b4f@posting.google.com...
> "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