From: Steve Tuckner Date: 2001-06-16T02:28:49+09:00 Subject: [ruby-talk:16499] Class Var question This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C0F5BF.C93F6E70 Content-Type: text/plain; charset="iso-8859-1" Below is a snippet of code with what I want to do. However it doesn't work as I would like. When I run it, I get {"test1"=>{"name"=>"Test 1"}, "test2"=>{"name"=>"Test 2"}, "test3"=>{"name"=>"Test 3"}} {"test1"=>{"name"=>"Test 1"}, "test2"=>{"name"=>"Test 2"}, "test3"=>{"name"=>"Test 3"}} What I want is: {"test1"=>{"name"=>"Test 1}} {"test2"=>{"name"=>"Test 2"}, "test3"=>{"name"=>"Test 3"}} What is the best way to get the class variable @@userVars defined in the sub-class (for each sub-class that sub-classes the super class). Also is there a way to also get this capability via a module mixin? Thanks //:) Steve Tuckner ---- (snip) ------------------------------- class Object @@userVars = Hash.new def Object.user(id, hash) module_eval <<-"end;" @@userVars[id.to_s] = hash end; end def userVars() return @@userVars end def userHash(name) return @@userVars[name] end end class Contact user :test1, { "name" => "Test 1" } attr_reader :test1 def initialize() @test1 = 1 end end class Task user :test2, { "name" => "Test 2" } user :test3, { "name" => "Test 3" } attr_reader :test2 attr_reader :test3 def initialize() @test2 = 2 @test3 = 3 end end a = Contact.new b = Task.new p a.userVars p b.userVars ------_=_NextPart_001_01C0F5BF.C93F6E70 Content-Type: text/html; charset="iso-8859-1"
Below is a snippet of code with what I want to do. However it doesn't work as I would like. When I run it, I get
 
{"test1"=>{"name"=>"Test 1"}, "test2"=>{"name"=>"Test 2"}, "test3"=>{"name"=>"Test 3"}}
{"test1"=>{"name"=>"Test 1"}, "test2"=>{"name"=>"Test 2"}, "test3"=>{"name"=>"Test 3"}}
 
What I want is:
 
{"test1"=>{"name"=>"Test 1}}
{"test2"=>{"name"=>"Test 2"}, "test3"=>{"name"=>"Test 3"}}
 
What is the best way to get the class variable @@userVars defined in the sub-class (for each sub-class that sub-classes the super class).
 
Also is there a way to also get this capability via a module mixin?
 
Thanks //:)
Steve Tuckner
---- (snip) -------------------------------
class Object
 @@userVars = Hash.new
 
   def Object.user(id, hash)
  module_eval <<-"end;"
   @@userVars[id.to_s] = hash
  end;
 end
 
 def userVars()
  return @@userVars
 end
 def userHash(name)
  return @@userVars[name]
 end
end
 
class Contact
 user :test1, { "name" => "Test 1" }
 attr_reader :test1
 
 def initialize()
  @test1 = 1
 end
end
 
class Task
 user :test2, { "name" => "Test 2" }
 user :test3, { "name" => "Test 3" }
 attr_reader :test2
 attr_reader :test3
 
 def initialize()
  @test2 = 2
  @test3 = 3
 end
end
 

a = Contact.new
b = Task.new
p a.userVars
p b.userVars
------_=_NextPart_001_01C0F5BF.C93F6E70--