Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > can't overload =

Reply
Thread Tools

can't overload =

 
 
Reacher
Guest
Posts: n/a
 
      07-03-2007
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

 
Reply With Quote
 
 
 
 
Chris Shea
Guest
Posts: n/a
 
      07-03-2007
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

 
Reply With Quote
 
 
 
 
james.d.masters@gmail.com
Guest
Posts: n/a
 
      07-03-2007
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
'='.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
UDP ports using PAT (NAT overload) - Help! Greg Grimes Cisco 8 10-08-2004 05:49 PM
UDP source ports using PAT (NAT overload) Greg Grimes Cisco 3 08-16-2004 10:26 PM
Using multiple outside interface with ip nat overload Emanuel Cisco 1 02-25-2004 08:10 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57