# MetaMonster

**URL:** <https://www.ruby-forum.com/t/metamonster/133482>\
**Category:** Ruby\
**Created:** [March 31, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/metamonster/133482 "2008-03-31T23:55:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [March 31, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/metamonster/133482/1 "2008-03-31T23:55:57Z")

</div>

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.se/173413](http://pastie.caboo.se/173413)

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.

---

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [April 1, 2008, 10:43am UTC](https://www.ruby-forum.com/t/metamonster/133482/2 "2008-04-01T10:43:16Z")

</div>

2008/3/31, Ruhe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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.

> 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

---

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [April 2, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/metamonster/133482/3 "2008-04-02T12:35:46Z")

</div>

On 1 ÁÐÒ, 12:42, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## outside. end other.#{s}"}.join(', ')})
> 
> use.inject do |as, often| as.you\_can - without end

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

---

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [April 2, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/metamonster/133482/4 "2008-04-02T13:29:02Z")

</div>

2008/4/2, Ruhe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On 1 ÁÐÒ, 12:42, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Here are my 0.02EUR:

> Thank you, Robert, for response.

You’re welcome!

> Your 0.02EUR are 100EUR for me 🙂

Oh, maybe I should start currency speculation… 🙂

Cheers

robert
