can't overload =

Discussion in 'Ruby' started by Reacher, Jul 3, 2007.

  1. Reacher

    Reacher Guest

    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
    Reacher, Jul 3, 2007
    #1
    1. Advertising

  2. Reacher

    Chris Shea Guest

    On Jul 3, 10:51 am, pietia <> 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
    Chris Shea, Jul 3, 2007
    #2
    1. Advertising

  3. Reacher

    Guest

    On Jul 3, 9:28 am, 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


    According to the PickAxe (2nd edition, pp. 338-9), you cannot override
    '='.
    , Jul 3, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    368
    Nick Hounsome
    Jan 25, 2004
  2. Atlas
    Replies:
    6
    Views:
    422
    Ron Natalie
    Dec 28, 2004
  3. Replies:
    0
    Views:
    5,584
  4. Replies:
    2
    Views:
    3,852
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    248
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page