Can you set Base class attributes from a subclass?

Hello,

I’m overriding the initialize method of a base class and I’m trying

to set a Base class variable in the overridden method and have it

register in the Base class (hope that makes sense).

Here’s my code:

[code]

in Rails::Generator::Base:

class Base

include Options

class_inheritable_accessor :spec

def initialize(runtime_args, runtime_options = {})

@args = runtime_args

parse!(@args, runtime_options)



# Derive source and destination paths.

@source_root = options[:source] || File.join(spec.path, 'templates')

end

end

class MediaModuleGenerator < Rails::Generator::Base

def initialize(runtime_args, runtime_options = {})

 self.spec.path = File.dirname(__FILE__) +

‘/generators/media_module’

 super

end

end

If you’re overriding the initialize method, then you should be

calling super before any other expression. This isn’t necessary

in regards to subclasses who parent is Object.

-Conrad

Hello,

I’m overriding the initialize method of a base class and I’m trying

to set a Base class variable in the overridden method and have it

register in the Base class (hope that makes sense).

Here’s my code:

[code]

in Rails::Generator::Base:

class Base

include Options

class_inheritable_accessor :spec

def initialize(runtime_args, runtime_options = {})

@args = runtime_args

parse!(@args, runtime_options)



# Derive source and destination paths.

@source_root = options[:source] || File.join(spec.path, 'templates')

end

end

class MediaModuleGenerator < Rails::Generator::Base

def initialize(runtime_args, runtime_options = {})

 self.spec.path = File.dirname(__FILE__) +

‘/generators/media_module’

 super

end

end

If you’re overriding the initialize method, then you should be

calling super before any other expression. This isn’t necessary

in regards to subclasses who parent is Object.

You should have an expression like this in your subclass:

super( runtime_args, runtime_options )

If you need further details, please consult the PixAxe book.

-Conrad

I tried adding

super( runtime_args, runtime_options )

and still got this error:

rails_generator/base.rb:107:in `initialize': You have a nil object when you didn't expect it!

my overidden initialize method now looks like this:

def initialize(runtime_args, runtime_options = {})   super( runtime_args, runtime_options ) end

Conrad Taylor wrote:

What’s on line 107 that’s generating the error message?

-Conrad

this is line 107:

@source_root = options[:source] || File.join(spec.path, 'templates')

Conrad Taylor wrote:

What’s the value of spec.path?