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

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

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):
    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>

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

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:

  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>

--
Matthew Kerwin
http://matthew.kerwin.net.au/

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:

    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
    <mailto:ruby-talk-request@ruby-lang.org>?subject=unsubscribe>
    <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

--
  Matthew Kerwin
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>

​Allow me to demonstrate with code:

​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?

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/