title: How to extend a class within a Ruby module
body: <p>I've been using OilyPNG (a set of C extensions for working with PNG's) and I'd like to start adding some of my own methods.  As such, I coded up a simple little C extension that creates the class <code>Hola</code> and includes the methods <code>Hola.bonjour</code> (which responds with a simple "hello") and <code>Hola.wazzup(input)</code> which spits the input right back. </p>
  
  <p>OilyPNG is a module that contains a single class <code>:Canvas</code> with a bunch of methods <code>:decode_png_image_pass</code>, <code>:from_data_url</code>, <code>:from_rgb_stream</code>, etc.).  I'd like to add my methods (<code>bonjour</code> and <code>wazzup</code>) to the <code>Canvas</code> so I can start using them.  How do I do this?  </p>
  
  <p>It seems like this is a very basic Ruby question, but I can't seem to find it addressed anywhere.  I've tried (among MANY other things) the following: </p>
  
  <pre><code>module ChunkyPNG
    class Canvas
      include Hola
    end
  end
  </code></pre>
  
  <p>...but it returns the following: </p>
  
  <pre><code>Error: #TypeError: wrong argument type Class (expected Module)
  </code></pre>
  
  <p>All I want to do is something simple like creating a new Canvas object, then running my method, i.e. <code>OilyPNG::Canvas.new.bonjour</code> and I'm totally stuck.</p>
  
  <p>UPDATE: The C code used to generate the extension</p>
  
  <pre><code>#include &lt;ruby.h&gt;
  
  /* our new native method; it just returns the string "bonjour!" */
  static VALUE hola_bonjour(VALUE self) {
      return rb_str_new2("bonjour, Zack!");
  }
  
  void Init_hola(void) {
      VALUE klass = rb_define_module("Hola"); /*DEFINED AS A MODULE*/
  
      rb_define_singleton_method(klass, "bonjour", hola_bonjour, 0);
  
  }
  </code></pre>
  
  <p>UPDATE 2:</p>
  
  <p>Finally got it working - using <code>rb_define module</code> in the C code, followed by defining the methods as <code>rb_define_method</code> (instead of as singletons).  Then build the gem, install it, then require the .so file, then open up OilyPNG to include the Hola module, as follows: </p>
  
  <pre><code>module OilyPNG
    class Canvas &lt; ChunkyPNG::Canvas
      def your_method
        puts "hello"
      end
        include Hola
    end
  end
  </code></pre>
  
  <p>Then load a png file as a test object: </p>
  
  <pre><code>test = OilyPNG::Canvas.from_file("C:/code/testfiles/orange.png"); nil
  </code></pre>
  
  <p>(the nil is to prevent Sketchup from crashing when it tries to send the PNG object to the command line), and then testing the output of:</p>
  
  <pre><code>test.bonjour
  </code></pre>
  
  <p>And it works!</p>