# Can you set Base class attributes from a subclass?

**URL:** <https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472>\
**Category:** Rails\
**Created:** [June 4, 2009, 4:39am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472 "2009-06-04T04:39:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 4:39am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/1 "2009-06-04T04:39:25Z")

</div>

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:

```auto

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

```

I should mention that spec is a module w/in the Rails framework IE:  
Rails::Generator::Spec

When I run my generator, I get this error:

You have a nil object when you didn’t expect it! (NoMethodError) The  
error occurred while evaluating nil.path

So when I set the self.spec.path var to File.dirname( **FILE** ) +  
‘/generators/media\_module’, it’s not registering in the base class.

Any ideas?

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 4:59am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/2 "2009-06-04T04:59:46Z")

</div>

On Wed, Jun 3, 2009 at 7:39 PM, Clem R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> in Rails::Generator::Base:
> 
> # Derive source and destination paths.
> 
> 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

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 5:03am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/3 "2009-06-04T05:03:51Z")

</div>

On Wed, Jun 3, 2009 at 7:58 PM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > @args = runtime\_args  
> > ‘/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

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 6:45am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/4 "2009-06-04T06:45:31Z")

</div>

On Wed, Jun 3, 2009 at 9:08 PM, Clem R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> my overidden initialize method now looks like this:
> 
> > wrote:
> > 
> > > calling super before any other expression. This isn’t necessary

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

-Conrad

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 6:08am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/5 "2009-06-04T06:08:20Z")

</div>

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 T. wrote:

> On Wed, Jun 3, 2009 at 7:58 PM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > @args = runtime\_args  
> > > ‘/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

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 7:38am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/6 "2009-06-04T07:38:09Z")

</div>

this is line 107:

@source\_root = options[:source] || File.join(spec.path, ‘templates’)

Conrad T. wrote:

> On Wed, Jun 3, 2009 at 9:08 PM, Clem R.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > my overidden initialize method now looks like this:
> > 
> > > wrote:
> > > 
> > > > calling super before any other expression. This isn’t necessary
> 
> What’s on line 107 that’s generating the error message?
> 
> -Conrad

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/7 "2009-06-04T13:54:36Z")

</div>

spec.path is nil.path - that’s the whole problem - I’m trying to set the  
spec.path = File.dirname( **FILE** ) + ‘/generators/media\_module’ in my  
overidden initialize method and I can’t seem to send that value to the  
Base class.

Conrad T. wrote:

> What’s the value of spec.path?
> 
> On Wed, Jun 3, 2009 at 10:38 PM, Clem R.  
> \<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [June 4, 2009, 8:24am UTC](https://www.ruby-forum.com/t/can-you-set-base-class-attributes-from-a-subclass/169472/8 "2009-06-04T08:24:25Z")

</div>

What’s the value of spec.path?

On Wed, Jun 3, 2009 at 10:38 PM, Clem R.  
\<removed\_email\_address@domain.invalid
