From: Jeremy Bopp Date: 2011-08-17T00:57:27+09:00 Subject: Re: Ruby inheriting from a class in another file. On 8/15/2011 09:02, Hunter Mcmillen wrote: > I moved all of the classes into their own files so now I have this: > proj/GoogleChart.rb > proj/BarChart.rb > proj/PieChart.rb > proj/GroupedBarChart.rb > > Now that they are in other files I am getting uninitialized constant > GoogleChart (NameError) in all of my subclasses on the line where I > inherit from GoogleChart, i.e. > > require 'GoogleChart' > BarChart < GoogleChart > Can anyone tell me what is wrong? The problem is not the require statement. If that had failed, you would get a LoadError rather than a NameError, so GoogleChart.rb loaded successfully. My guess is that your GoogleChart class is actually within a deeper namespace, something like Your::Namespace::GoogleChart. Perhaps when you had everything in the single file, that namespace was enclosing all your class definitions, so the look up of the GoogleChart name just worked. Now that you broke everything apart, that namespace apparently isn't wrapping the definitions in the new file. There are 4 solutions I see offhand: 1) Wrap the definition of BarChart within the same namespace as GoogleChart. e.g. require 'GoogleChart' module Your module Namespace class BarChart < GoogleChart ... end end end 2) Explicitly name the full name of GoogleChart. e.g. require 'GoogleChart' class BarChart < Your::Namespace::GoogleChart ... end 3) Include the namespace of GoogleChart into main. e.g. require 'GoogleChart' include Your::Namespace class BarChart < GoogleChart ... end 4) Remove the enclosing namespace from the definition of GoogleChart. -Jeremy