Is it possible to override the assignment operator?
I tried “def operator=”, and while the compiler seems to accept the
syntax, my method is not called at run time
Thanks in advance…
Is it possible to override the assignment operator?
I tried “def operator=”, and while the compiler seems to accept the
syntax, my method is not called at run time
Thanks in advance…
What would be the receiver?
Variables aren’t objects, so they can’t have methods.
Variables merely point to objects.
You can define methods with names that end in equals signs, if you
want.
If you do that at the outer scope (i.e. as a private method in
Object), it might look like you’ve overridden assignment:
def xyz=(something)
p something
end
xyz = 123
But, of course, there’s no variable named xyz.
On Friday 02 August 2002 01:55 pm, Reginald Braithwaite-Lee wrote:
Is it possible to override the assignment operator?
I tried “def operator=”, and while the compiler seems to accept the
syntax, my method is not called at run time
–
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE
It works, but you’re just not calling it the right way (though this is
probably not what you wanted):
class A
def operator=(something)
p “here i am #{something}”
end
end
a = A.new
a.operator = 123
On Friday 02 August 2002 01:55 pm, Reginald Braithwaite-Lee wrote:
I tried “def operator=”, and while the compiler seems to accept the
syntax, my method is not called at run time
–
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE
Ned Konz ned@bike-nomad.com wrote in message news:200208021420.20760@ned.bike-nomad.com…
On Friday 02 August 2002 01:55 pm, Reginald Braithwaite-Lee wrote:
I tried “def operator=”, and while the compiler seems to accept the
syntax, my method is not called at run timeIt works, but you’re just not calling it the right way (though this is
probably not what you wanted):class A
def operator=(something)
p “here i am #{something}”
end
enda = A.new
a.operator = 123
Thanks for your help. As you guessed, that wasn’t what I wanted,
because I wanted special behaviour that would always work when
assignment was made.
Oh well.