I'm trying to write a program that dynamically defines ruby classes based on configuration read from a file. I know I can use Class.new to do this. Here's an example program:
x = [1,2,3]
Test = Class.new do
@@mylist = x
def foo
puts @@mylist
end
end
Test.new.foo
When I run this I get the following output (running with ruby 1.9.3p0):
c:/utils/test.rb:4: warning: class variable access from toplevel
c:/utils/test.rb:7: warning: class variable access from toplevel
1
2
3
Does anyone know what causes these warnings and how I can get rid of them?
I've tried replacing the line tjhat does
@@mylist = x
with this
class_variable_set(:@@mylist, x)
But when I do that I get this error instead:
c:/utils/test.rb:7: warning: class variable access from toplevel
c:/utils/test.rb:7:in `foo': uninitialized class variable @@mylist in Object (NameError)
from c:/utils/test.rb:11:in `'
Thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…