Skip to content

Instantly share code, notes, and snippets.

@hanshasselberg
Created September 4, 2012 08:26
Show Gist options
  • Save hanshasselberg/3618489 to your computer and use it in GitHub Desktop.
Save hanshasselberg/3618489 to your computer and use it in GitHub Desktop.
Configuration
# your_lib.rb
module YourLib
def self.configure
yield Config
end
end
# your_lib/config.rb
module YourLib
module Config
extend self
attr_accessor :option1, :option2
end
end
# setting
YourLib.configure do |config|
config.option1 = 1
end
YourLib::Config.option2 = 2
# getting
YourLib::Config.option1
#=> 2
YourLib::Config.option2
#=> 2
@kronn
Copy link

kronn commented Sep 4, 2012

extend self comes unexpected. Kind of scary, but results in a nice API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment