GOTO Kentaro <gotoken / notwork.org> wrote in message news:<20010811.195841.112625335.gotoken / notwork.org>...
> From: furufuru / ccsr.u-tokyo.ac.jp (Ryo Furue)
> Subject: [ruby-talk:19517] Why not?: Assigning to self
> Date: Sat, 11 Aug 2001 19:43:29 +0900
> >       self = res
> >    end
> > end
> > 
> > The line "self = res" gives an error
> > 
> >    smooth:17: Can't change the value of self
> >          self = res
> 
> You can use "replace(res)" instead of "self = res". 
[...]

Thank you for the answer!  That solves my current problem.

Now the "why" part of the question remains.  I'm curious about why an
assignment to self is forbidden.  Is it to avoid surprises? or is it
related to some technical problem?  I could imagine that if it were
allowed, there could be lots of surprises; for example,

    class Array
       def meth
          s = "hello"
          self = s
       end
    end

    a = [3.14, 2.9]
    a.meth
    puts a   # prints "hello" !!

But, what if an assignment to self is allowed only when both sides
refer to objets of the same class?  This question is only out of
curiosity.

Cheers,
Ryo