How is does this module code work?

Posted by phsr on Stack Overflow See other posts from Stack Overflow or by phsr
Published on 2010-06-06T15:19:39Z Indexed on 2010/06/06 15:22 UTC
Read the original article Hit count: 213

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about ruby

Related posts about class