[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

can't overload =

Reacher

7/3/2007 4:28:00 PM

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

3 Answers

pietia

7/3/2007 4:51:00 PM

0

Reacher wrote:
> 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
>

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 !

Chris Shea

7/3/2007 5:03:00 PM

0

On Jul 3, 10:51 am, pietia <pietia....@gmail.com> wrote:
> Reacher wrote:
> > 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
>
> 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

james.d.masters

7/3/2007 7:58:00 PM

0

On Jul 3, 9:28 am, Reacher <brandon.g.jo...@gmail.com> wrote:
> 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
'='.