tags:

views:

567

answers:

3

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

I want to instantiate Only_Onions in the module Shish so that all the classes extending Shish can use the object to deteremine 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 an error "uninitialized constant Shish::Only_Onions (NameError). What does that mean?

A: 

I want to instantiate Only_Onions in the module Shish so that all the classes extending Shish can use the object to deteremine if they have only__onions.

Uh, what? A code sample is needed.

apostlion
A: 

try

::Only_Onions
John Douthat
I thought the :: is only when you have a module name before it and a class name after it to designate a namespace? What does it do when you use it without a module in front of it? I'm not familiar with this.
pez_dispenser
A blank namespace indicates the global namespace. So Object is both "Object" and "::Object".
Chuck
+3  A: 

The order of declaration has an effect. Shish doesn't know about Only_Onions in your code. If you change it to this, then Only_Onions is already declared when you define the module Shish:

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
pez_dispenser
This solved the original problem. But I think the only_onions_class needs to be a class variable. Am i correct? Can I still make the above code work correctly?
kunjaan