On 10/8/07, ara.t.howard <ara.t.howard / gmail.com> wrote: > > On Oct 8, 2007, at 9:16 AM, Dave Baldwin wrote: > > > I want to use class variables and have them accessible from class > > methods that have been defined outside of the class using > > define_method or something similar. When I try this the class > > variable isn't in scope. A simple test case is: > > > > class A > > @@aa = 20 NOTE @@ > > end > > > > A.class_eval {define_method(:foo){puts @aa}} NOTE @ Another thing, in addition to Ara's point about the lexical scope of the closure, the original posting was mixing class variables with instance variables: class A @@aa = 20 # This is a class variable def aa= puts @aa # This is a reference to an INSTANCE variable puts @@aa # This is a reference to a CLASS variable end end There's no relationship between @aa and @aa, any more that there would be with aa or $aa Note that Ruby also has class instance variables: class A def cv_aa @@aa end def cv_aa=(val) @@aa = val end def iv_aa=(val) @aa = val end def iv_aa @aa end def self.civ_aa @aa end def self.civ_aa=(val) @aa = val end end class B < A def set_cv_aa=(val) @@aa = val end end a = A.new a2 = A.new b = B.new # Instance variable belong to the instance a.iv_aa = "Hello" a.iv_aa # => "Hello" a2.iv_aa # => nil a2.iv_aa = "Ciao!" a2.iv_aa # => "Ciao!" a.iv_aa # => "Hello" a2.cv_aa = 30 a2.cv_aa # => 30 # Class variables are shared a.cv_aa = 20 a.cv_aa # => 20 a2.cv_aa # => 20 #and with subclasses b.cv_aa # => 20 b.cv_aa = 30 b.cv_aa # => 30 a.cv_aa # => 30 # Class instance variables are instance variables of classes A.civ_aa = "Classy!" A.civ_aa # => "Classy!" B.civ_aa = "Chic!" B.civ_aa # => "Chic!" A.civ_aa # => "Classy!" -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/