On Mon, 24 Mar 2003 11:14:40 +0900, Greg McIntyre wrote:
> dblack / superlink.net wrote:
>> (This might also have implications for the equivalence you draw
>> between "self.attribute" in Python and "@attribute" in Ruby, but
>> I'm not sure exactly what's going on on the Python side there.)
> 'self' is the first parameter in a Python method. Because Ruby
> doesn't do things this way, (perhaps something I should note on
> the slides), it has to define 'self' as a special constant, but
> also has the @ prefix as a shortcut (for 'self.').

@foo and self.foo are two different things. @foo refers to the
variable; self.foo refers to a method 'foo'. 

> class A
>   def method(arg0, arg1, ..., argN)
>     @x = something
>     self.x = something # also valid
>   end
> end

irb(main):001:0> class A
irb(main):002:1>   def method(arg)
irb(main):003:2>     @x = arg
irb(main):004:2>     self.x = arg
irb(main):005:2>   end
irb(main):006:1> end
=> nil
irb(main):007:0> a = A.new
=> #<A:0x283f468>
irb(main):008:0> a.method(1)
NameError: undefined method `x=' for #<A:0x283f468>
        from (irb):4:in `method'
        from (irb):8
irb(main):009:0>

self.foo (and self.foo=) is only usable if you have
  attr_accessor :foo
somewhere in the class definition.

-austin
-- Austin Ziegler, austin / halostatue.ca on 2003.03.23 at 21:42:21