[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

MetaMonster

Ruhe

3/31/2008 9:53:00 PM

Hi.
I needed something like a Vector class,
but with named elements, and default params in constructor.
I needed to add, subtract, multiply my own vectors.

At the same time a learned a lot about metaprogramming.
So I decided to make my own monster :)
http://pastie.caboo...

I don't hope that my work is useful in real-world app,
but I would like to see critics of my metaprogramed monster.
What is wrong, what could be done better.

PS
I understand that this is a bad way to build class,
I did it to get new skills in metaprogramming, not for real-world use.
3 Answers

Robert Klemme

4/1/2008 8:42:00 AM

0

2008/3/31, Ruhe <nocturneer@gmail.com>:
> Hi.
> I needed something like a Vector class,
> but with named elements, and default params in constructor.
> I needed to add, subtract, multiply my own vectors.

That instantly reminds of Struct.

> At the same time a learned a lot about metaprogramming.
> So I decided to make my own monster :)
> http://pastie.caboo...
>
> I don't hope that my work is useful in real-world app,
> but I would like to see critics of my metaprogramed monster.
> What is wrong, what could be done better.
>
> PS
> I understand that this is a bad way to build class,
> I did it to get new skills in metaprogramming, not for real-world use.

Here are my 0.02EUR:

Make create and others class instance methods instead of instance
methods. You do not need the instance for anything else than creating
classes so it's obsolete. At least it should not be visible from the
outside.

You could save some typing by integrating this with Struct, i.e. let
Struct do all that it does already and only add mathematic methods,
like

class Vector
def self.new(*syms)
cl = Struct.new(*syms)
# add math instance methods to cl
cl
end
end

I'd probably also generate the method code and class_eval it vs. using
instance_variable_set and *get because that code is likely faster and
you do not need a closure, e.g.

%w{+ -}.each do |op|
cl.class_eval = %Q{
def #{op}(other)
self.class.new(#{syms.map {|s| "self.#{s} #{op}
other.#{s}"}.join(', ')})
end
}
end

Ah, and why the many empty lines? Or is this just a pasting artifact?

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Ruhe

4/2/2008 10:32:00 AM

0

On 1 ???, 12:42, Robert Klemme <shortcut...@googlemail.com> wrote:
>
> > PS
> > I understand that this is a bad way to build class,
> > I did it to get new skills in metaprogramming, not for real-world use.
>
> Here are my 0.02EUR:
>
> Make create and others class instance methods instead of instance
> methods. You do not need the instance for anything else than creating
> classes so it's obsolete. At least it should not be visible from the
> outside.
>
> You could save some typing by integrating this with Struct, i.e. let
> Struct do all that it does already and only add mathematic methods,
> like
>
> class Vector
> def self.new(*syms)
> cl = Struct.new(*syms)
> # add math instance methods to cl
> cl
> end
> end
>
> I'd probably also generate the method code and class_eval it vs. using
> instance_variable_set and *get because that code is likely faster and
> you do not need a closure, e.g.
>
> %w{+ -}.each do |op|
> cl.class_eval = %Q{
> def #{op}(other)
> self.class.new(#{syms.map {|s| "self.#{s} #{op}
> other.#{s}"}.join(', ')})
> end
> }
> end
>
> Ah, and why the many empty lines? Or is this just a pasting artifact?
>
> Kind regards
>
> robert
>
> --
> use.inject do |as, often| as.you_can - without end



Thank you, Robert, for response.
Your 0.02EUR are 100EUR for me :)

Robert Klemme

4/2/2008 11:28:00 AM

0

MjAwOC80LzIsIFJ1aGUgPG5vY3R1cm5lZXJAZ21haWwuY29tPjoKPiBPbiAxIMHQ0iwgMTI6NDIs
IFJvYmVydCBLbGVtbWUgPHNob3J0Y3V0Li4uQGdvb2dsZW1haWwuY29tPiB3cm90ZToKPiAgPgo+
ICA+IEhlcmUgYXJlIG15IDAuMDJFVVI6Cgo+IFRoYW5rIHlvdSwgUm9iZXJ0LCBmb3IgcmVzcG9u
c2UuCgpZb3UncmUgd2VsY29tZSEKCj4gIFlvdXIgMC4wMkVVUiBhcmUgMTAwRVVSIGZvciBtZSA6
KQoKT2gsIG1heWJlIEkgc2hvdWxkIHN0YXJ0IGN1cnJlbmN5IHNwZWN1bGF0aW9uLi4uIDotKQoK
Q2hlZXJzCgpyb2JlcnQKCi0tIAp1c2UuaW5qZWN0IGRvIHxhcywgb2Z0ZW58IGFzLnlvdV9jYW4g
LSB3aXRob3V0IGVuZAo=