From: Ken Bloom Date: 2008-05-02T09:40:13+09:00 Subject: Re: DSL - class-level methods that affect instances On Thu, 01 May 2008 18:07:11 -0500, Daniel Waite wrote: > I'm having a problem of design. I've tried a few configurations of class > collaboration and responsibility, but they all end up feeling messy. > > I'm looking at these (either or, my original attempt was with a module, > but attr_accessor *looks* like it does what I want): > > class C < Translator::Base > translate 'this', :to => 'that' > end > > class C > include Translator > translate 'this', :to => 'that' > end > > With the expectation that... > > c = C.new > c.translations > > Would yield a hash of the from and to values. > > I have no idea how to break the barrier between class and instance. :( Use class variables (the @@ variety). class Translator::Base def self.translate word, params @@translations ||= {} @@translations[word]=params[:to] end def translations @@translations ||= {} end end class C < Translator::Base translate 'a', :to => 'b' translate 'c', :to => 'd' end C.new.translations #=> {"a"=>"b", "c"=>"d"} -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/