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
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.
Not quite - I think j += 1 only evaluates j once. I recall one instance
where that helped me, performance-wise.
···
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
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.
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:
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).
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.
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)
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).
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” ]