I am trying to write a program that dynamically defines ruby ββclasses based on the configuration read from a file. I know I can use Class.new for this. Here is 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 (works 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
one
2
3
Does anyone know what causes these warnings and how can I get rid of them?
I tried replacing the tjhat string
@@mylist = x
with this
class_variable_set(:@@mylist, x)
But when I do this, I get this error:
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!
oop ruby dynamic warnings
yoda_alex Nov 04 2018-11-11T00: 00Z
source share