I'm new to ruby, but very excited. I'm stymied on an issue of mixing in a class variable. Let me describe the whole situation; maybe somebody with a better overall picture of Ruby can answer.
I want to do a mixin that lets you define what the appearance of a string representation of an object will look like, so it's easy to print the object to a file, let user edit it, and read it back.
I had in mind for a mixin to declare a class variable, a list of field descriptors with a label, a type, and which instance variable is printed that way. Types would be string, numeric, list (string or numeric) and text (where all but text are dumped in the form "[fieldname] value\n"
, and text is allowed to be multi-line: "[fieldname]\ntext". This makes an intuitive form for users to edit.
My problem is, if I define the creation of this list that contains these field types in a mixin, it creates it as a class variable to the class I mixed it into, as I intended; but when I go to use it in the class I defined in that module, the class can't find it because it's in the wrong namespace. I can't seem to figure out how I will be able to refer to it, since the namespace it's really in will depend on what class I mixed the module into.
module Dumper
BEGIN { @@dumplist = Array.new }
def dump
result = ""
@@dumplist.each { |i| result += i.dump }
end
end
class Dumpfield
def initialize(name,type,value)
@name,@type,@value = name,type,value
end
def dump
result = "[#{@name}]"
case type
when "string","numeric"
result += " #{@value}"
when "list","numlist"
result += " "
@value.each { |i| result += "#{i}, "}
when "text"
result += "\n#{@value}"
end
end
end
class TestDumper
BEGIN {
require "dumper"
include Dumper
@@dumplist<<Dumpfield.new("firstitem","string",@field1)
# ... and other entries in @@dumplist
}
def initialize(a)
@field1 = a
end
end
If I try dump on an instance of testDumper, I get an error
on the dump call inside Dumper, saying it can't find @@dumplist. What do I do? Am I on the wrong track?
I want to do a mixin that lets you define what the appearance of a string representation of an object will look like, so it's easy to print the object to a file, let user edit it, and read it back.
I had in mind for a mixin to declare a class variable, a list of field descriptors with a label, a type, and which instance variable is printed that way. Types would be string, numeric, list (string or numeric) and text (where all but text are dumped in the form "[fieldname] value\n"
, and text is allowed to be multi-line: "[fieldname]\ntext". This makes an intuitive form for users to edit.
My problem is, if I define the creation of this list that contains these field types in a mixin, it creates it as a class variable to the class I mixed it into, as I intended; but when I go to use it in the class I defined in that module, the class can't find it because it's in the wrong namespace. I can't seem to figure out how I will be able to refer to it, since the namespace it's really in will depend on what class I mixed the module into.
module Dumper
BEGIN { @@dumplist = Array.new }
def dump
result = ""
@@dumplist.each { |i| result += i.dump }
end
end
class Dumpfield
def initialize(name,type,value)
@name,@type,@value = name,type,value
end
def dump
result = "[#{@name}]"
case type
when "string","numeric"
result += " #{@value}"
when "list","numlist"
result += " "
@value.each { |i| result += "#{i}, "}
when "text"
result += "\n#{@value}"
end
end
end
class TestDumper
BEGIN {
require "dumper"
include Dumper
@@dumplist<<Dumpfield.new("firstitem","string",@field1)
# ... and other entries in @@dumplist
}
def initialize(a)
@field1 = a
end
end
If I try dump on an instance of testDumper, I get an error
on the dump call inside Dumper, saying it can't find @@dumplist. What do I do? Am I on the wrong track?