Skip to content

Instantly share code, notes, and snippets.

@quamen
Created May 9, 2012 12:00
Show Gist options
  • Save quamen/2644014 to your computer and use it in GitHub Desktop.
Save quamen/2644014 to your computer and use it in GitHub Desktop.
A module that allows you to conditionally define a module. Useful for isolated testing, when you just need something to stub without requiring the actual class or module in the test.
class ConditionallyDefine
def self.stub_module(full_name)
full_name.to_s.split(/::/).inject(Object) do |context, name|
begin
context.const_get(name)
rescue NameError
context.const_set(name, Module.new)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment