From: David Alan Black Date: 2001-08-15T03:21:51+09:00 Subject: [ruby-talk:19716] Re: class methods and optimizing Hello -- On Wed, 15 Aug 2001, Chris Moore wrote: > The last one here is easy... I remember from somewhere that self is the > default object. So can I remove it from class definitions completely and > it's just there to save confusion while reading? (Sorry brain on the > fritz....*bang* *bang*....) You can usually do without it, but not always. In particular, you need it when you're doing assignments that might involve local variables or might be "=" methods. Here are some examples: class A attr_accessor :thing def initialize(n) thing = n # local var; you need self.thing= or @thing = end def talk puts thing # ok because no assignment preceded it end def localtalk thing = "I'm a local var" puts thing # assignment means this is a local 'thing' end def weirdtalk puts thing # that's the method thing() thing = "weird!" # assignment creates local var! puts thing # which is this 'thing' end def myclass puts self.class # just "class" won't parse end end a = A.new(123) a.talk # => nil a.thing = "hi" a.talk # => hi a.localtalk # => I'm a local var a.weirdtalk # => hi # weird! Amendations, clarifications, further cases welcome. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav