From: Mathieu Bouchard Date: 2002-01-14T05:37:27+09:00 Subject: Re: MetaRuby : RubySchema.rb howto? On Sat, 12 Jan 2002, Tobias Reif wrote: > Mathieu Bouchard wrote: > > I'm making incompatible changes to it about right now. Its main current > > purpose is that interested people can look at it and suggest > > changes. > to_s, but something like that seems to be on your list already. #to_s already has a meaning; it will be named differently. #inspect reproduces the code necessary to reproduce the same objects like this: x = Ruby::M[Ruby::LAtom[2], :+, [Ruby::LAtom[2]], nil, nil]; x.inspect #==> Ruby::M[Ruby::LAtom[2], :+, [Ruby::LAtom[2]], nil, nil] What you want would look like this: x.to_ruby #==> 2.+(2) But I'm not sure about the naming at all. > I am. For example, I'm writing a program that's generating Ruby code. It > works, but some code could be made much cleaner by using objects > representing the components of the program to be generated. > So where I now have a mess of spaces, newlines, strings etc. > def make_getter attr_name > meth_name = to_methodname(attr_name) > "\n def #{meth_name}\n"+ > " @attributes['#{attr_name}'].to_s.to_number\n"+ > " end\n" > end this is already nicer: def make_getter attr_name meth_name = to_methodname(attr_name) %{ def #{meth_name} @attributes['#{attr_name}'].to_s.to_number end } end > , I'd like to write something like > def make_getter attr_name > DefMethod.new(...) > # ... > end def make_getter attr_name sel = to_methodname(attr_name).intern DefMethod[nil,sel,DefArgs[[],nil,nil], Body[[ M[ M[ SliceVar[:@attributes,[LAtom[attr_name]],nil], :to_s,[],nil,nil], :to_number,[],nil,nil]]]]] end I think you'll still prefer sticking with the text-based version unless version 0.8 gets _really_ simpler. (At least it's not XML.) by the way, I'd like you to be able to do something like this one day: def make_getter attr_name meth_name = to_methodname(attr_name) %c{ def %u{meth_name} @attributes[%u{LAtom[attr_name]}].to_s.to_number end } end Where %c{} and %u{} would be similar to %{} and #{} except that %c{} would generate code-objects, not strings. (and so would launch the ruby parser recursively...) > > You won't be able to do that. ClassDefs don't contain methods directly, > > they contain MethodDefs (or not) instead. When you will eval > why not "to_s"? (Or did I get something wrong here) #to_ruby (or whatever) will give you the source. evaling that will give you a real class with real methods. ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju