# Sample for Metaprogramming

**URL:** <https://www.ruby-forum.com/t/sample-for-metaprogramming/150696>\
**Category:** Ruby\
**Created:** [October 11, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696 "2008-10-11T18:40:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 11, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/1 "2008-10-11T18:40:38Z")

</div>

Hi.

Can anyone give me a good small sample for Metapgramming with Ruby? I  
am new in Ruby and searching now samples to get a look into Ruby (I  
also use IronRuby - when there will be a small sample which is also  
working with IronRuby this would be wonderful).

Thanks!

Fridrich

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 11, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/2 "2008-10-11T19:38:35Z")

</div>

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone give me a good small sample for Metapgramming with Ruby?

There you go:

require ‘fileutils’  
module Files  
extend self  
def in(dir)  
dir = File.expand\_path(dir)

```
Class.new do
  extend Enumerable
  @@dir = dir

  class <<self; self; end.class_eval do
    alias old_new new
    private :old_new

    def new(filename, contents=nil, &blk)
      if contents
        open("#{dirname}/#{filename}", "w") do |f|
          f.write contents
        end
      elsif blk
        open("#{dirname}/#{filename}", "w", &blk)
      else
        open("#{dirname}/#{filename}", "w") {}
      end
      old_new(filename)
    end

    def dirname() @@dir end

    def each()
      Dir.glob("#{dirname}/*") {|f| yield old_new(f)}
    end

    def to_s()
      "#{dirname}"
    end

    def inspect
      "Directory: #{self}"
    end

    Dir.glob("#{dir}/*") do |file|
      define_method(File.basename file) do
        old_new(File.basename file)
      end
    end
  end

  def initialize(filename)
    @filename = filename
  end

  def path()
    "#{self.class.dirname}/#{@filename}"
  end
  alias to_s path

  def inspect
    "File: #{self}"
  end

  def open(*args, &blk)
    Kernel.open(path, *args, &blk)
  end

  def delete
    FileUtils.rm(path)
    name = @filename
    class <<self.class; self end.class_eval do
      undef_method(name)
    end
  end
end

```

end  
end

Ok, this took way more time to write than I expected and also turned out  
quite  
a bit bigger and isn’t really tested very thoroughly. But the idea is a  
such:

Using Files.in(dirname) you can get a class representing that directory.  
This class will have a method for every file in that directory.  
Calling such a method will give you an instance of the class,  
representing the  
specific file.  
Calling new(filename) on the class will create a file in that directory.  
The class is extended with Enumerable and you can use it to iterate over  
its  
instances representing the files that exist the directory.

Of course this is quite a complicated way to work with files, but you  
wanted  
meta-programming, so there you go.  
(I did intend this to be simpler than it turned out to be, though)

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 12, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/3 "2008-10-12T22:39:59Z")

</div>

On Oct 11, 2008, at 11:37 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone give me a good small sample for Metapgramming with Ruby?

The Pragmatic Programmers have a series of screencasts about this:

[http://www.pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming](http://www.pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 11, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/4 "2008-10-11T20:15:28Z")

</div>

On Oct 11, 2008, at 10:37 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

> **[Ruby Quiz - metakoans.rb (#67)](http://rubyquiz.com/quiz67.html)**

[http://kinderman.net/articles/2007/09/22/learning-ruby-meta-programming-with-metakoans](http://kinderman.net/articles/2007/09/22/learning-ruby-meta-programming-with-metakoans)

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 14, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/5 "2008-10-14T20:39:31Z")

</div>

On Sun, Oct 12, 2008 at 02:35:34AM +0900, Sebastian H. wrote:

> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Can anyone give me a good small sample for Metapgramming with Ruby?
> 
> There you go:

[snip]

> instances representing the files that exist the directory.
> 
> Of course this is quite a complicated way to work with files, but you wanted  
> meta-programming, so there you go.  
> (I did intend this to be simpler than it turned out to be, though)

Is there anyone here that knows Lisp well enough to provide a Lisp  
example that does the same thing, just for purposes of comparing how the  
two languages handle these techniques?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/6 "2008-10-15T17:31:41Z")

</div>

Chad P. wrote:

> Is there anyone here that knows Lisp well enough to provide a Lisp  
> example that does the same thing, just for purposes of comparing how the  
> two languages handle these techniques?

That’s pretty complicated. The first step would be to define what “the  
same  
thing” is in lisp. Lisp doesn’t really have the concept of calling  
methods on  
an object, so you’d have to come up with something else. And at that  
point it  
would be kinda hard to compare the implementations, because they’d  
implement  
somewhat different things.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/7 "2008-10-15T20:24:58Z")

</div>

On Thu, Oct 16, 2008 at 12:27:05AM +0900, Sebastian H. wrote:

> Chad P. wrote:
> 
> > Is there anyone here that knows Lisp well enough to provide a Lisp  
> > example that does the same thing, just for purposes of comparing how the  
> > two languages handle these techniques?
> 
> That’s pretty complicated. The first step would be to define what “the same  
> thing” is in lisp. Lisp doesn’t really have the concept of calling methods on  
> an object, so you’d have to come up with something else. And at that point it  
> would be kinda hard to compare the implementations, because they’d implement  
> somewhat different things.

I’m aware of the existence of CLOS, but don’t really know anything about  
its specifics of implementation and use. How does it handle the kind of  
functionality that, in Ruby, consists of sending messages to objects to  
invoke their methods?

Should we take this off-list?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2008, 12:16am UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/8 "2008-10-16T00:16:56Z")

</div>

Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Should we take this off-list?

I’m enjoying following this discussion on list, if you do take it off,  
please include me. Thanks – Eric

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2008, 1:52am UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/9 "2008-10-16T01:52:21Z")

</div>

On Thu, Oct 16, 2008 at 04:53:04AM +0900, Sebastian H. wrote:

> Chad P. wrote:
> 
> > I’m aware of the existence of CLOS, but don’t really know anything about  
> > its specifics of implementation and use. Â How does it handle the kind of  
> > functionality that, in Ruby, consists of sending messages to objects to  
> > invoke their methods?
> 
> It doesn’t, really. CLOS methods are overloadable functions basically, i.e.  
> you can define multiple methods of the same name, but with different argument  
> types, and it’ll pick the right one to call depending on the type(s) of the  
> object(s) you pass as arguments. There’s no concept of a receiver though.

So, basically, instead of real methods, CLOS just uses . . . what?  
Parametric polymorphism? This isn’t meant as a challenge or complaint,  
but rather just a question so I’ll understand it better:

What about CLOS makes it “object oriented”?

> > Should we take this off-list?
> 
> Well, since we’re discussing CLOS in the context of comparing it to ruby, it’s  
> still kinda on-topic, so unless someone complains, I see no reason not to  
> continue this discussion here on the list.

Works for me.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2008, 1:55am UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/10 "2008-10-16T01:55:52Z")

</div>

On Thu, Oct 16, 2008 at 07:16:10AM +0900, Eric S. wrote:

> Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > Should we take this off-list?
> 
> I’m enjoying following this discussion on list, if you do take it off,  
> please include me. Thanks – Eric

Noted.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2008, 3:52am UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/11 "2008-10-16T03:52:24Z")

</div>

Chad P. wrote:

> So, basically, instead of real methods, CLOS just uses . . . what?  
> Parametric polymorphism? This isn’t meant as a challenge or complaint,  
> but rather just a question so I’ll understand it better:

IIRC: CLOS uses generic functions, aka multimethod dispatch. Think of a  
hash table that maps n-tuples of classes to implementation functions.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 15, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/12 "2008-10-15T21:53:44Z")

</div>

Chad P. wrote:

> I’m aware of the existence of CLOS, but don’t really know anything about  
> its specifics of implementation and use. How does it handle the kind of  
> functionality that, in Ruby, consists of sending messages to objects to  
> invoke their methods?

It doesn’t, really. CLOS methods are overloadable functions basically,  
i.e.  
you can define multiple methods of the same name, but with different  
argument  
types, and it’ll pick the right one to call depending on the type(s) of  
the  
object(s) you pass as arguments. There’s no concept of a receiver  
though.

> Should we take this off-list?

Well, since we’re discussing CLOS in the context of comparing it to  
ruby, it’s  
still kinda on-topic, so unless someone complains, I see no reason not  
to  
continue this discussion here on the list.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 16, 2008, 3:58am UTC](https://www.ruby-forum.com/t/sample-for-metaprogramming/150696/13 "2008-10-16T03:58:51Z")

</div>

Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > object(s) you pass as arguments. There’s no concept of a receiver though.
> 
> So, basically, instead of real methods, CLOS just uses . . . what?  
> Parametric polymorphism? This isn’t meant as a challenge or complaint,  
> but rather just a question so I’ll understand it better:

The following begins with a good overview of the rational behind the use  
of generic functions in CLOS instead of using a message passing system  
as used in ruby.

> **[ECOOP.pdf](https://dreamsongs.com/Files/ECOOP.pdf)**
>
> 102.18 KB

linked to from

> **[Common Lisp Object System](https://dreamsongs.com/CLOS.html)**

It makes the good point that generic functions are a natural solution in  
any lisp. The paper states…

| Furthermore, in Common Lisp, arithmetic operators are already  
| generic in a certain sense. The expression  
|  
| (+ x y)  
|  
| does not imply that x and y are of any particular type, nor does it  
| imply that they are of the same type. For example, x might be an  
| integer and y a complex number, and the + operation is required to  
| perform the correct coercions and produce an appropriate result.  
|  
| Because the Object System is a system for Common Lisp, it is  
| important that generic functions be first-class objects and that the  
| concept of generic functions be an extension and a generalization of  
| the concept of Common Lisp functions. In this way, the Object System  
| is a natural and smooth extension of Common Lisp.

Also, CLOS contains mechanisms for implementing a message-passing based  
object system. Again from the paper…

| Despite the advantages of generic functions, there may at times be  
| reasons for preferring a message-passing style; such a style can be  
| implemented by means of generic functions or by means of the Common  
| Lisp Object System Meta-Object Protocol.

I would very much like to see any implementation of a ruby-like message  
based object system using CLOS.

> What about CLOS makes it “object oriented”?

The fact that it provides for the creation of objects which are  
instances of classes, and provides for inheritance between classes. 🙂

– Eric
