From: dblack@... Date: 2006-01-21T10:38:06+09:00 Subject: Re: Inserting Hi -- On Sat, 21 Jan 2006, James Hughes wrote: > On 1/20/06, dblack@wobblini.net wrote: >> Hi -- >> >> On Sat, 21 Jan 2006, James Hughes wrote: >> >>> On 1/19/06, Paul Brannan wrote: >>>> >>>> You can also create a mixin and extend Foo::Bar with the mixin. If you >>>> do this, instance methods in the mixin will be available as class >>>> methods in Foo::Bar and derived classes. >>> >>> Ok, I'm doing something like the following: >>> >>> module FooBarHelper >>> >>> class << Foo::Bar >>> # some instance methods, which reference class vars from Foo::Bar >>> end >>> end >>> >>> Then, in classes A and B I do "include FooBarHelper". When I run this >>> I get "uninitialized class variable" errors for the Foo::Bar's class >>> vars. >>> >>> So, to state my question again, how can I extend the functionality of >>> the Foo::Bar library (short of hacking it's source directly), while >>> still maintaining access to the class variables defined in Foo::Bar? >>> Is this even possible? >> >> Does this help at all? [...] > Hmm. Not sure yet. While I try to wrap my head around what's going on > with the const_missing and const_get magic above, here is the short > script I wrote to try and simulate the real-world problem I'm trying > to solve. Note that the derived class (A) is trying to access a class > variable and not a constant as in your example. Whoops, I misread. > module Foo > class Bar > @@classvar = "Boo!" > end > end > > module FooBarHelper > class << Foo::Bar > def amethod > puts @@classvar The class variables of Foo::Bar's singleton class are not the same as those of Foo::Bar, so you'd want to avoid opening up the singleton class. You can do this instead: class Foo::Bar def self.amethod puts @@classvar end end As things stand there's no need for the FooBarHelper module at all: class A < Foo::Bar end A.amethod # Boo! David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black