# Turning Dwemthy's Array into a mixin

**URL:** <https://rubytalk.org/t/turning-dwemthys-array-into-a-mixin/42559>\
**Category:** ruby-talk\
**Created:** [28 November 2007 02:35 UTC](https://rubytalk.org/t/turning-dwemthys-array-into-a-mixin/42559 "2007-11-28T02:35:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Choi](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Joshua\_Choi](https://rubytalk.org/u/Joshua_Choi)\
**Post date:** [28 November 2007 02:35 UTC](https://rubytalk.org/t/turning-dwemthys-array-into-a-mixin/42559/1 "2007-11-28T02:35:00Z")

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

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

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

&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor(\*given\_traits)

&nbsp;&nbsp;&nbsp;&nbsp;given\_traits.each do |trait|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;metaclass.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(trait) do |trait\_value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@traits ||= {}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@traits[trait] = trait\_value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:initialize) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.traits.each do |k, v|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set("@#{k}", v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

end

---

<div class="post-metadata">

**Author:** ![McLaughlin\_James\_T](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@McLaughlin\_James\_T](https://rubytalk.org/u/McLaughlin_James_T)\
**Post date:** [28 November 2007 16:22 UTC](https://rubytalk.org/t/turning-dwemthys-array-into-a-mixin/42559/2 "2007-11-28T16:22:37Z")

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

> **···**
>
> -----Original Message-----  
> From: [ruby-talk-admin@ruby-lang.org](mailto:ruby-talk-admin@ruby-lang.org) [[mailto:ruby-talk-admin@ruby-lang.org](mailto:ruby-talk-admin@ruby-lang.org)]  
> On Behalf Of [rbysamppi@gmail.com](mailto:rbysamppi@gmail.com)  
> Sent: Tuesday, November 27, 2007 9:35 PM  
> To: ruby-talk ML  
> Subject: Turning Dwemthy's Array into a mixin
> 
> 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
> 
> &nbsp;&nbsp;def self.metaclass  
> &nbsp;&nbsp;&nbsp;&nbsp;class \<\< self; self; end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def self.traits(\*given\_traits)  
> &nbsp;&nbsp;&nbsp;&nbsp;return @traits if given\_traits.empty?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor(\*given\_traits)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;given\_traits.each do |trait|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;metaclass.instance\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(trait) do |trait\_value|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@traits ||= {}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@traits[trait] = trait\_value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;class\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:initialize) do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.class.traits.each do |k, v|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set("@#{k}", v)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> end

---

<div class="post-metadata">

**Author:** ![\_why](https://avatars.discourse-cdn.com/v4/letter/_/73ab20/32.png) [@\_why](https://rubytalk.org/u/_why)\
**Post date:** [28 November 2007 17:09 UTC](https://rubytalk.org/t/turning-dwemthys-array-into-a-mixin/42559/3 "2007-11-28T17:09:18Z")

</div>

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

> **···**
>
> On Wed, Nov 28, 2007 at 11:35:00AM +0900, rbysamppi@gmail.com wrote:
> 
> > As a beginner, I'm having trouble converting the metaprogramming in  
> > Dwemthy's Array ([http://poignantguide.net/dwemthy/\](http://poignantguide.net/dwemthy/%5C)) 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?
