Q:

Avoiding globals with dynamically loaded ruby files

I am in a position at the moment where I have a plugins folder where there there could be 1 or 100 plugins to be loaded.
Now the problem is, each plugin requires an instance of a class defined within the startup ruby file.
A really simplified example would be:
#startup.rb

def load_plugins
    #... get each plugin file
    require each_plugin
end

class MuchUsedClass
    def do_something
        #...
    end
end

muchUsedInstance = MuchUsedClass.new
load_plugins

#some_plugin.rb

class SomePluginClass
    def initialize(muchUsedInstance)
        @muchUsedInstance = muchUsedInstance
    end

    def do_something_with_instance
        @muchUsedInstance.do_something
    end
end

somePluginInstance = SomePluginClass.new(muchUsedInstance)
somePluginInstance.do_something_with_instance

The main problem is that when you call require, it doesn't have any clue about what has happened before it is being required. So I find it nasty making a global variable within the startup file just to satisfy all other required files, but it seems like one of the only ways to be able to pass some data down to an included file, I could also make a singleton class to expose some of this, but that also seems a bit nasty.
As I am still new to ruby and am still looking through the statically typed glasses, I will probably be missing a decent pattern to solve this, in C# I would opt for dependency injection and just hook everything up that way...

A:

Your example code does not have a global variable.  Global variables have names that start with $.  The code as you wrote it won't work, because muchUsedInstance is just a local variable and will not be shared between different Ruby files.
If you are not going to change the instance ever, you could easily store it as a constant:
MuchUsedInstance = MuchUsedClass.new

You could store it as a nested constant inside the class:
MuchUsedClass::Instance = MuchUsedClass.new

You could store it as an instance variable inside the class object, with a getter method that automatically creates it if it isn't there already:
def MuchUsedClass.instance
  @instance ||= MuchUsedClass.new
end

