title: Ruby metaprogramming: How can make module method see class's variable
body: <p>For example I have a module and a class:</p>
  
  <pre><code>module SimpleModule
    def self.class_hello
      puts "hello from #{@@name}"
    end
  end
  
  class SimpleClass
    @@name = 'StackOverFlow'
    def self.test
      SimpleModule.class_hello
    end
  end
  </code></pre>
  
  <p>Then I test by calling module method from class:</p>
  
  <pre><code>SimpleClass.test
  </code></pre>
  
  <p>I meet exception:</p>
  
  <blockquote>
    <p>uninitialized class variable @@name in SimpleModule (NameError)</p>
  </blockquote>
  
  <p>I know here because scope of module is not same as class scope. So my question is: How can I share SimpleClass scope for SimpleModule scope? </p>
  
  <p>I put metaprogramming because here is just simple example, after that I will advanced by calling dynamic module from dynamic class. (that is the reason why I don't want to use some keyword such as <strong>include</strong> or <strong>extend</strong>)</p>
  
  <p><strong>@Edit</strong>
  In fact I want to implement Ruby extends on my own. Here is my already developed version:</p>
  
  <pre><code># implementation
  class Class
    def custom_extend(module_name)
      module_name.methods(false).each do |method|
        define_singleton_method(method) do |*args, &amp;block|
          module_name.send(method, *args, &amp;block)
        end
      end
    end
  end
  </code></pre>
  
  <p>And here is my custom module and class for testing:</p>
  
  <pre><code># -------------------------------------------------------------
  # Demonstration
  module SimpleModule
    def self.class_hello_world
      puts 'i am a simple module boss'
    end
  
    def self.class_hello_name
      puts "hello from #{@@name}"
    end
  end
  
  class SimpleClass
    custom_extend  SimpleModule
    @@name = 'StackOverFlow'
  end
  </code></pre>
  
  <p>Here is my two test:</p>
  
  <pre><code>SimpleClass.class_hello_world  # work
  SimpleClass.class_hello_name   # not work
  </code></pre>