Feature #9060

"Assignment" operator overloading

Added by Bruce Metz 6 months ago. Updated 6 months ago.

[ruby-core:58073]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:-

Description

=begin
I understand assignment in ruby is assigning an object to the variable. However there are other cases we need to use assignments. For example:

class Foo
end

class Bar < Foo
end

f = Foo.new
b = Bar.new
//Some operations on b

Now we want ((|f|)) to contain the same data as ((|b|)). But if we use (({f = b})), ((|f|)) will not be (({Foo})) any more.
Or we can define a method like (({Foo##loadFromBar})), but (({Foo})) is the super class of (({Bar})) and usually defined before (({Bar}))...

Can we have something like:
f := b
so that ((|f|)) will have the same shared data with ((|b|))?

And we can even load data from other classes by overloading this operator:

class Foo
def :=(f)
@data = f
end
end
f = Foo.new
f := 2.2

We can always achieve this by defining a named method, but this operator can provide some syntax sugar and convenience.
=end

History

#1 Updated by Yukihiro Matsumoto 6 months ago

  • Status changed from Open to Rejected

I am not sure what you want. Could you elaborate?
Doesn't defining regular method like #update do for you?

class Foo
def update(obj)
@data = obj.data
end
attr_accessor :data
end
class Bar < Foo
end

f = Foo.new
b = Bar.new
....
f.update(b)

Reopen if you have objection.

Matz.

#2 Updated by Nobuyoshi Nakada 6 months ago

  • Description updated (diff)

Also available in: Atom PDF