How is does this module code work?
- by phsr
I'm new to ruby and I am trying to figure out how the following code works
The following code is inside a class in a module. The method is called later with the following code: @something ||= Module::Class.config
class << self
def config &block
options = OpenStruct.new
yield options if block_given?
init_client! Client.new(options)
end
def init_client!(client)
base_eigenclass = class << Base; self; end
base_eigenclass.send :define_method, :client do
@client = client
end
client
end
end
The class has some constants in it, and when the classes initialize is called, the instance member are set to option.variable || VARIABLE_CONSTANT. I understand that if there is no value for option.variable then VARIABLE_CONSTANT is used, but I don't understand that calling Module::Class.config do |options| #some block end set the @client until config is called again with options
The code definitely works, but I want to understand how it does