En rñÑonse Chris Eskow : > Hi, I'm semi-new to Ruby and I have a question. > > I have a class, World, that basically looks like this: > > class World > > # Convenience method that takes one or more symbols and defines an > # accessor+mutator method for each one. > def self.att *atts > atts.each do |att| > class_eval %{ > def #{att} val=nil > val == nil ? @#{att} : @#{att} = val > end > } > end > end > > # Now I can define some attributes. > att :title, :author > This looks a lot like the traits library. You might want to look at it. > > The att method creates an instance method that sets an attribute if > an argument is given, or returns it if no arguments are given. This > is nice and all, but now I want to use this att method with another > class. Since it's very short I could just copy and paste it, but I'd > rather use a module, Attributes, that I could include into any class: > > class World > include Attributes > att :title, :author > end > > class Thing > include Attributes > att :name, :desc > end > > I'm having trouble getting it to work, however. I have a feeling I > have to use metaclasses or something, but I'm not quite sure how I'm > suppose to do that. Everything I tried resulted in "undefined method > `att'" errors. Any thoughts? > What you need is the att method to become a class method rather than an instance method, so that you can use it like one uses attr_accessor. To do that, simply use "extend" instead of "include": class World extend Attributes att :title, :author end Another way that uses metaclasses indeed is to include your module in the metaclass of the class: class OtherWorld class <<self include Attributes end att :title, :author end I'm not aware of any difference between those two ways. -- Christophe Grandsire. http://rainbow.conlang.free.fr You need a straight mind to invent a twisted conlang.