0

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.

1
  • 2
    by nesting you get the prefix for free, ie MyModule::ErrorClass, that is valid ruby syntax - in your first example it's not. Commented Oct 19, 2018 at 14:25

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.