# "superclass must be a Class (NilClass given)" when attempting to use class method "\[\]"

**URL:** <https://rubytalk.org/t/superclass-must-be-a-class-nilclass-given-when-attempting-to-use-class-method/73671>\
**Category:** ruby-talk\
**Created:** [16 June 2016 01:04 UTC](https://rubytalk.org/t/superclass-must-be-a-class-nilclass-given-when-attempting-to-use-class-method/73671 "2016-06-16T01:04:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Graham\_Menhennitt1](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@Graham\_Menhennitt1](https://rubytalk.org/u/Graham_Menhennitt1)\
**Post date:** [16 June 2016 01:04 UTC](https://rubytalk.org/t/superclass-must-be-a-class-nilclass-given-when-attempting-to-use-class-method/73671/1 "2016-06-16T01:04:44Z")

</div>

I've been looking at Rails 5.0. It uses an interesting Ruby construct: in class Migration, there is method defined as "def self.[](version)". Now, that's just defining the usual "[]" operator on the class. It's then used like (paraphrased):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class X \< Migration[5]  
to allow versioning of the Migration class. That seems to make sense.

It works correctly in Rails, but I can't get it to work in my code.

class SetIndexedValue  
&nbsp;&nbsp;&nbsp;def self.[](indexSizeBytes)  
&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;...  
end

class X \< SetIndexedValue[2]  
end

This produces an error: superclass must be a Class (NilClass given) (TypeError) on the line "class X...". I'm using Ruby 2.2.5 for both my Rails code and for the code above.

Can anybody please tell me what I'm doing wrong?

Thanks,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Graham

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [16 June 2016 01:12 UTC](https://rubytalk.org/t/superclass-must-be-a-class-nilclass-given-when-attempting-to-use-class-method/73671/2 "2016-06-16T01:12:23Z")

</div>

Are you returning the class from your . method?

> **···**
>
> On 16 June 2016 at 11:04, Graham Menhennitt \<graham@menhennitt.com.au\> wrote:
> 
> > I've been looking at Rails 5.0. It uses an interesting Ruby construct: in  
> > class Migration, there is method defined as "def self.(version)". Now,  
> > that's just defining the usual "" operator on the class. It's then used  
> > like (paraphrased):  
> > &nbsp;&nbsp;&nbsp;&nbsp;class X \< Migration[5]  
> > to allow versioning of the Migration class. That seems to make sense.
> > 
> > It works correctly in Rails, but I can't get it to work in my code.
> > 
> > class SetIndexedValue  
> > &nbsp;&nbsp;def self.(indexSizeBytes)  
> > &nbsp;&nbsp;...  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;...  
> > end
> > 
> > class X \< SetIndexedValue[2]  
> > end
> > 
> > This produces an error: superclass must be a Class (NilClass given)  
> > (TypeError) on the line "class X...". I'm using Ruby 2.2.5 for both my  
> > Rails code and for the code above.
> > 
> > Can anybody please tell me what I'm doing wrong?
> > 
> > Thanks,  
> > &nbsp;&nbsp;&nbsp;&nbsp;Graham
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Gnzls](https://avatars.discourse-cdn.com/v4/letter/g/cc9497/32.png) [@Gnzls](https://rubytalk.org/u/Gnzls)\
**Post date:** [16 June 2016 02:25 UTC](https://rubytalk.org/t/superclass-must-be-a-class-nilclass-given-when-attempting-to-use-class-method/73671/3 "2016-06-16T02:25:54Z")

</div>

blockquote, div.yahoo\_quoted { margin-left: 0 !important; border-left:1px #715FFA solid !important; padding-left:1ex !important; background-color:white !important; } What?

> **···**
>
> Sent from Yahoo Mail for iPad
> 
> On Wednesday, June 15, 2016, 6:12 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> 
> Are you returning the class from your . method?  
> On 16 June 2016 at 11:04, Graham Menhennitt \<graham@menhennitt.com.au\> wrote:
> 
> &nbsp;&nbsp;I've been looking at Rails 5.0. It uses an interesting Ruby construct: in class Migration, there is method defined as "def self.(version)". Now, that's just defining the usual "" operator on the class. It's then used like (paraphrased):  
> class X \< Migration[5]  
> to allow versioning of the Migration class. That seems to make sense.
> 
> It works correctly in Rails, but I can't get it to work in my code.
> 
> class SetIndexedValue  
> def self.(indexSizeBytes)  
> ...  
> end  
> ...  
> end
> 
> class X \< SetIndexedValue[2]  
> end
> 
> This produces an error: superclass must be a Class (NilClass given) (TypeError) on the line "class X...". I'm using Ruby 2.2.5 for both my Rails code and for the code above.
> 
> Can anybody please tell me what I'm doing wrong?
> 
> Thanks,  
> Graham
> 
> Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> 
> --  
> Matthew Kerwin  
> [http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Graham\_Menhennitt1](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@Graham\_Menhennitt1](https://rubytalk.org/u/Graham_Menhennitt1)\
**Post date:** [16 June 2016 01:53 UTC](https://rubytalk.org/t/superclass-must-be-a-class-nilclass-given-when-attempting-to-use-class-method/73671/4 "2016-06-16T01:53:33Z")

</div>

That was it. Thank you!

Graham

> **···**
>
> On 16/06/2016 11:12 AM, Matthew Kerwin wrote:
> 
> > Are you returning the class from your . method?
> > 
> > On 16 June 2016 at 11:04, Graham Menhennitt \<graham@menhennitt.com.au \> \<mailto:graham@menhennitt.com.au\>\> wrote:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;I've been looking at Rails 5.0. It uses an interesting Ruby  
> > &nbsp;&nbsp;&nbsp;&nbsp;construct: in class Migration, there is method defined as "def  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.(version)". Now, that's just defining the usual ""  
> > &nbsp;&nbsp;&nbsp;&nbsp;operator on the class. It's then used like (paraphrased):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class X \< Migration[5]  
> > &nbsp;&nbsp;&nbsp;&nbsp;to allow versioning of the Migration class. That seems to make sense.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;It works correctly in Rails, but I can't get it to work in my code.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;class SetIndexedValue  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.(indexSizeBytes)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;class X \< SetIndexedValue[2]  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;This produces an error: superclass must be a Class (NilClass  
> > &nbsp;&nbsp;&nbsp;&nbsp;given) (TypeError) on the line "class X...". I'm using Ruby 2.2.5  
> > &nbsp;&nbsp;&nbsp;&nbsp;for both my Rails code and for the code above.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Can anybody please tell me what I'm doing wrong?
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Thanks,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Graham
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<mailto:ruby-talk-request@ruby-lang.org\>?subject=unsubscribe\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> > 
> > --  
> > &nbsp;&nbsp;Matthew Kerwin  
> > [http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [16 June 2016 02:49 UTC](https://rubytalk.org/t/superclass-must-be-a-class-nilclass-given-when-attempting-to-use-class-method/73671/5 "2016-06-16T02:49:39Z")

</div>

​Allow me to demonstrate with code:

```ruby
​irb(main):001:0> def foo
irb(main):002:1> end
=> :foo
irb(main):003:0> class X < foo
irb(main):004:1> end
TypeError: superclass must be a Class (NilClass given)
from (irb):3
from /usr/bin/irb:11:in `<main>'
irb(main):005:0> def bar
irb(main):006:1> Object
irb(main):007:1> end
=> :bar
irb(main):008:0> class Y < bar
irb(main):009:1> end
=> nil
irb(main):010:0> Y.new
=> #<Y:0x000000027f47a0>

```

I hope this hint is enough to trigger a small epiphany.

Cheers

> **···**
>
> On 16 June 2016 at 12:25, Gnzls \<gonzales@flash.net\> wrote:
> 
> > What?
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
