Uninitialized constant MyClass (NameError) in Ruby

I have a Shish module (which acts as an abstract class) and a Only_Onions visitor class.

I want to create an instance of Only_Onions in the Shish module so that all classes extending Shish can use the object to determine if they have only__onions.

module Shish only_onions_class = Only_Onions.new end class Only_Onions def for_skewer return true end end class Skewer include Shish def only_onions return only_onions_class.for_skewer end def veg? return true end end 

But I get the error "uninitialized constant Shish :: Only_Onions (NameError). What does this mean?

+6
ruby
source share
2 answers

The order of the announcement has an effect. Shish does not know about Only_Onions in your code. If you change it to this, Only_Onions is already declared when you define the Shish module:

 class Only_Onions def for_skewer return true end end module Shish only_onions_class = Only_Onions.new end class Skewer include Shish def only_onions return only_onions_class.for_skewer end def veg? return true end end 
+6
source share

to try

 ::Only_Onions 
+1
source share

All Articles