can't overload =

R

Reacher

simple class:

class Myclass
def initialize(a)
@a = a
end

def prt
puts @a.to_s
end

def =(a)
@a = a
self
end
end

produces:

main.rb:23: syntax error, unexpected '='
def =(a)
^
main.rb:28: syntax error, unexpected kEND, expecting $end
 
C

Chris Shea

hi !

Maybe you should try this way:

def MyClass=
@a = a
self
end

and the testing code :

a = Myclass.new(10)
b = a
b.prt => 10 :)

pietia !

I do not think this means what you think it means.

your MyClass= method is not being used on the second line of your
testing code "b = a". That's just the standard assignment operator.
That "testing" code evaluates the same without the MyClass= method
(that method is never called, and is, as it looks to me, nonsensical).

I think the correct answer is: you cannot override =

HTH,
Chris
 
J

james.d.masters

simple class:

class Myclass
def initialize(a)
@a = a
end

def prt
puts @a.to_s
end

def =(a)
@a = a
self
end
end

produces:

main.rb:23: syntax error, unexpected '='
def =(a)
^
main.rb:28: syntax error, unexpected kEND, expecting $end

According to the PickAxe (2nd edition, pp. 338-9), you cannot override
'='.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top