title: Using define_method and meta-programming to define instance methods dynamically in Ruby?
body: <p>I have some code something like : </p>
  
  <pre><code>class Country
    attr_reader :name
  
    def initialize
      @name     = "MyName".freeze
    end
  
    def government
      @government ||= Government.new(self)
    end
  
    def symbols
      @symbols ||= Symbols.new(self)
    end
  
    def economy
      @economy ||= Economy.new(self)
    end
  
    def education
      @education ||= Education.new(self)
    end
  
    def healthcare
      @healthcare ||= Healthcare.new(self)
    end
  
    def holidays
      @holidays ||= Holidays.new(self)
    end
  
    def religion
      @religion ||= Religion.new(self)
    end
  
  end
  </code></pre>
  
  <p>How can I create the methods dynamically? I tried : </p>
  
  <pre><code>class Country
    attr_reader :name
  
    COMPONENETS = %w(government symbols economy education healthcare holidays religion)
  
  
    COMPONENETS.each do |m|
      define_method(m) do |argument|
        instance_variable_set("@#{m}",Object.const_get(m.capitalize).new(self))
      end
    end
  
    def initialize
      @name     = "MyName".freeze
    end
  
  end 
  </code></pre>
  
  <p>If I try: </p>
  
  <p><code>puts Country.new.education.inspect</code></p>
  
  <p>I get the following error: </p>
  
  <pre><code>country.rb:16:in `block (2 levels) in &lt;class:Country&gt;': wrong number of arguments (0 for 1) (ArgumentError)
      from country.rb:27:in `&lt;main&gt;'
  </code></pre>
  
  <p>What am I missing here? </p>