Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class vars in mixins? 1

Status
Not open for further replies.

bkhull

Programmer
May 13, 2001
2
0
0
US
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(&quot;firstitem&quot;,&quot;string&quot;,@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?
 
'include' only adds the constants and methods of the module, not the class variables. however, you can, in this case, use a constant instead of an @@ variable. this will work because while ruby issues a warning if you change the actual reference of a constant (that is, since all variables and constants are just references to objects, ruby will throw a warning if you alter the ACTUAL value of the constant), it doesn't mind at all if you play with the object being referred to (through methods like '<<', '[]', &c.). in your case, just change @@dumplist to Dumplist and use it the same. it should still work.

also, there may be a way to include the class variables using something other than 'include', like append_features or extend_object, but i don't have my pick-axe book right now, and library computers don't usually have ruby installed on them (although they should), so i'll look it up when next i get a chance. i also remember a similar question being asked faily recently (last week or so) on the mailing list ruby-talk, so searching their archives couldn't hurt too much (except that the word 'include' will likely be in a quarter of all posts anyway...^_^).

hope this helps.
-stillflame &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
both of these ruby-talk threads deal with including:
but it's not really what you're looking for. in fact, one of the two even mentions that class vars are included with a call to include, but i couldn't produce the behavior with my ruby (1.6.2), so i don't really know. those threads should still be of some use - the demonstration of how to use append_features by Dave Thomas looked fairly promising... &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top