synapse
synapse

Reputation: 5728

Returning dynamically generated module from a method

I have a bunch of classes with similiar logic like this

class ApiWrapper
  class << self
    attr_accessor :app_id, :app_key

    def configure
      yield self
    end
  end
end

I want to extract this logic to a module similar to Ruby Struct class to be able to do something like this

class ApiWrapper
  include Configurable.instance :app_id, :app_key
end

How can I do this?

Upvotes: 1

Views: 70

Answers (1)

andrykonchin
andrykonchin

Reputation: 2517

From documentation

fred = Module.new do
  def meth1
    "hello"
  end
  def meth2
    "bye"
  end
end

Upvotes: 1

Related Questions