Sample for Metaprogramming

R

relzne

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
 
S

Sebastian Hungerecker

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 :eek:ld_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
 
S

Sebastian Hungerecker

Chad said:
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.
 
S

Sebastian Hungerecker

Chad said:
I'm aware of the existence of CLOS, but don't really know anything about
its specifics of implementation and use. =A0How 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.=
=20
you can define multiple methods of the same name, but with different argume=
nt=20
types, and it'll pick the right one to call depending on the type(s) of the=
=20
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, i=
t's=20
still kinda on-topic, so unless someone complains, I see no reason not to=20
continue this discussion here on the list.

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
E

Eric Schulte

Chad Perrin said:
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
 
J

Joel VanderWerf

Chad said:
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.
 
E

Eric Schulte

Chad Perrin said:
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.

http://www.dreamsongs.com/Files/ECOOP.pdf

linked to from

http://www.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
|=20
| (+ x y)
|=20
| 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.
|=20
| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top