# Turning Dwemthy's Array into a mixin

**URL:** <https://www.ruby-forum.com/t/turning-dwemthys-array-into-a-mixin/121448>\
**Category:** Ruby\
**Created:** [November 28, 2007, 3:35am UTC](https://www.ruby-forum.com/t/turning-dwemthys-array-into-a-mixin/121448 "2007-11-28T03:35:47Z")\
**Posts on this page:** 3\
**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:** [November 28, 2007, 3:35am UTC](https://www.ruby-forum.com/t/turning-dwemthys-array-into-a-mixin/121448/1 "2007-11-28T03:35:47Z")

</div>

As a beginner, I’m having trouble converting the metaprogramming in  
Dwemthy’s Array ([http://poignantguide.net/dwemthy/](http://poignantguide.net/dwemthy/)) into a convenient  
mixin that I can use for many classes. The trouble seems to be the  
“def self.” declarations, but I’m not too sure; in any  
case, .metaclass and .traits don’t seem to be mixing in. What should I  
change?

Thanks in advance!

module Dwemthy

def self.metaclass  
class \<\< self; self; end  
end

def self.traits(\*given\_traits)  
return @traits if given\_traits.empty?

```
attr_accessor(*given_traits)

given_traits.each do |trait|
  metaclass.instance_eval do
    define_method(trait) do |trait_value|
      @traits ||= {}
      @traits[trait] = trait_value
    end
  end
end

class_eval do
  define_method(:initialize) do
    self.class.traits.each do |k, v|
      instance_variable_set("@#{k}", v)
    end
  end
end

```

end

end

---

<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:** [November 28, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/turning-dwemthys-array-into-a-mixin/121448/2 "2007-11-28T17:25:11Z")

</div>

You might take a look at the metaid gem

Gem install metaid

It contains some of those meta classes - I use it for creating  
Activerecord  
classes on the fly for data driven XML ingestion.

Jamie

---

<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:** [November 28, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/turning-dwemthys-array-into-a-mixin/121448/3 "2007-11-28T18:09:40Z")

</div>

On Wed, Nov 28, 2007 at 11:35:00AM +0900, removed\_email\_address@domain.invalid wrote:

> As a beginner, I’m having trouble converting the metaprogramming in  
> Dwemthy’s Array ([http://poignantguide.net/dwemthy/](http://poignantguide.net/dwemthy/)) into a convenient  
> mixin that I can use for many classes. The trouble seems to be the  
> “def self.” declarations, but I’m not too sure; in any  
> case, .metaclass and .traits don’t seem to be mixing in. What should I  
> change?

Hey, so, yes, this is what Dwemthy has been talking about all along,  
this is exactly what caused him to start grinding his teeth and his  
blood pressure went through the roof and he got a contagious clubbed  
foot and so on, simply because he didn’t know or have the slightest  
inkling concerning append\_features.

Mixins only mix instance methods. However, our friend Nobu Nakada  
has just the trick. →   
[http://redhanded.hobix.com/bits/hyperextended.html](http://redhanded.hobix.com/bits/hyperextended.html)

Basically, put all the class methods in their own module. Then mix  
the module into the Dwemthy module using `extend`. Then add an  
`append_features` method to the Dwemthy module that mixes both into  
future mixees. It’s a bit of a trip, but still only about five  
extra lines of code.

Nobu lays low, but he’s filthy smart that one.

\_why
