title: question regarding define_method and method_missing
body: <p>How can I make this code work?</p>
  
  <pre><code>class Meta
  
      @array = [:a,:b]
  
      def self.method_missing(name, *args, &amp;block)
              if @array.include? name
                      self.class.send(:define_method, name) do
                              do_call(name)
                      end
              else
                      puts "[#{name.inspect}] is not part of array!"
              end
      end
  
      def do_call arg
              puts "doing call for ['#{arg}'] "
      end
  end
  </code></pre>
  
  <p>The idea is to have Meta.a (after being defined) to have </p>
  
  <pre><code>def a 
   do_call(a)
  end
  </code></pre>
  
  <p>in its body. But after running this, my output is:</p>
  
  <blockquote>
    <p>[:do_call] is not part of array!</p>
  </blockquote>
  
  <p>UPDATE: now kinda working like this:</p>
  
  <pre><code>class Meta
  
      @array = [:a,:b]
  
      def self.do_call arg
              puts "doing call for ['#{arg}'] "
      end
  
      def self.method_missing(name, *args, &amp;block)
              puts "#{name} is missing!"
              if @array.include? name
                      self.class.send(:define_method, name) do
                              do_call name
              end
              else
                      puts "[#{name.inspect}] is not part of array!"
              end
         end
      end
  </code></pre>
  
  <p>but still, here is an excerpt of an IRB session:</p>
  
  <blockquote>
    <p>[~/code] $ irb -r meta</p>
    
    <p>irb(main):001:0> Meta.a</p>
    
    <p>a is missing!</p>
    
    <p>=> #</p>
    
    <p>irb(main):002:0> Meta.a</p>
    
    <p>doing call for ['a']</p>
    
    <p>=> nil</p>
    
    <p>irb(main):003:0> c = Meta.new</p>
    
    <p>=> #</p>
    
    <p>irb(main):004:0> c.a</p>
    
    <p>NoMethodError: undefined method `a' for #</p>
  
  <pre><code>    from (irb):4
  </code></pre>
    
    <p>irb(main):005:0> Meta.methods</p>
    
    <p>=> ["inspect", "send", "pretty_inspect", "class_eval", "clone", "yaml_tag_read_class", > >"public_methods", "protected_instance_methods", "<strong>send</strong>", "private_method_defined?", 
    "equal?", "freeze", "do_call", "yaml_as", "methods", "instance_eval", "to_yaml", "display", 
    "dup", "object_id", "include?", "private_instance_methods", "instance_variables", "extend", 
    "protected_method_defined?", "const_defined?", "to_yaml_style", "instance_of?", "eql?", 
    "name", "public_class_method", "hash", "id", "new", "singleton_methods", 
    "yaml_tag_subclasses?", "pretty_print_cycle", "taint", "pretty_print_inspect", "frozen?", 
    "instance_variable_get", "autoload", "constants", "kind_of?", "to_yaml_properties", "to_a", 
    "ancestors", "private_class_method", "const_missing", "type", "yaml_tag_class_name", 
    "instance_method", "&lt;", "protected_methods", "&lt;=>", "instance_methods", "==", 
    "method_missing", "method_defined?", "superclass", ">", "pretty_print", "===", 
    "instance_variable_set", "const_get", "is_a?", "taguri", ">=", "respond_to?", "to_s", "&lt;=", 
    "module_eval", "class_variables", "allocate", "class", "taguri=", 
    "pretty_print_instance_variables", "tainted?", "public_instance_methods", "=~", 
    "private_methods", "public_method_defined?", "autoload?", "<strong>id</strong>", "nil?", "untaint", 
    "included_modules", "const_set", "a", "method"]</p>
  </blockquote>
  
  <p>what gives? 'a' is a class method and it is not passed on to the new Meta object (c). Why?</p>