> 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
>
[different third class deleted]
> 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, but the third
will be accessing @bar, not @@bar. (Try it in IRB and you'll see.)
Interestingly though, the the sexps representing the AST for the first two
classes are pretty different looking:
[[:class,
:Example,
:Object,
[:defn,
:example,
[:scope,
[:block,
[:args],
[:defs, [:self], :bar, [:scope, [:block, [:args], [:cvar, :@@bar]]]],
[:defs,
[:self],
:bar=,
[:scope, [:block, [:args, :val], [:cvasgn, :@@bar, [:lvar, :val]]]]]]]]]
vs.
[[:class,
:Example,
:Object,
[:defn,
:example,
[:scope,
[:block,
[:args],
[:sclass,
[:self],
[:scope,
[:block,
[:defn, :bar, [:scope, [:block, [:args], [:cvar, :@@bar]]]],
[:defn,
:bar=,
[:scope,
[:block, [:args, :val], [:cvasgn, :@@bar, [:lvar, :val]]]]]]]]]]]]]
···
On 4/10/06, Austin Ziegler <halostatue@gmail.com> wrote:
On 4/10/06, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:
-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca
--
thanks,
-pate
-------------------------