From: dblack@... Date: 2006-04-11T03:21:30+09:00 Subject: Re: Quick syntax question Hi -- On Tue, 11 Apr 2006, Mark Volkmann wrote: > On 4/10/06, Austin Ziegler wrote: >> On 4/10/06, Gregory Seidman wrote: >>> Consider the following three class definitions: >>> >>> class Foo >>> def self.bar >>> @@bar >>> end >>> def self.bar=(val) >>> @@bar = val >>> end >>> end >>> >>> class Foo >>> class << self >>> def bar >>> @@bar >>> end >>> def bar=(val) >>> @@bar = val >>> end >>> end >>> end >>> >>> class Foo >>> class << self >>> attr_accessor :bar >>> end >>> end >>> >>> Am I correct in believing that they are equivalent? If not, how do they >>> differ? >> >> They are not equivalent. The first two are the same, > > That's good to know. I can't see any reason to prefer the second over > the first. It's a lot more noisy. There's a subtle difference between the two ways of defining a singleton method, though: X = 1 class C end class << C X = 2 end class << C def x X end end def C.x2 X end p C.x # 2 (the X from C's singleton class) p C.x2 # 1 (the X from outer scope) David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" coming in PDF April 15, and in paper May 1! http://www.manning.com/black