Picachieu
Picachieu

Reputation: 3782

Inheritance failing when class name is prefixed with module name

I'm having a really weird problem with inheritance within a module. Here's my code:

module MyModule
    class MyModule.ErrorClass < StandardError

    end
end

When I run it, I get this error:

myfile.rb:2: syntax error, unexpected '<', expecting &. or :: or '[' or '.'
        class MyModule.ErrorClass < StandardError
                               ^
myfile.rb:5: syntax error, unexpected keyword_end, expecting end-of-input

However, when I change it to this:

module MyModule
    class ErrorClass < StandardError

    end
end

it runs fine with no errors.

Upvotes: 0

Views: 97

Answers (1)

spickermann
spickermann

Reputation: 106802

There is no need to repeat the module name as you did in your first example. And furthermore using a . instead of :: to separate the module name from the class name is not valid Ruby.

Just use

module MyModule
  class ErrorClass < StandardError
    # ...
  end
end

or

class MyModule::ErrorClass < StandardError # note the colons
  # ...
end

Upvotes: 2

Related Questions