# Get mixin's name

**URL:** <https://www.ruby-forum.com/t/get-mixins-name/51279>\
**Category:** Ruby\
**Created:** [December 29, 2005, 2:19am UTC](https://www.ruby-forum.com/t/get-mixins-name/51279 "2005-12-29T02:19:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [December 29, 2005, 2:19am UTC](https://www.ruby-forum.com/t/get-mixins-name/51279/1 "2005-12-29T02:19:38Z")

</div>

A somewhat weird situation.

I have a module which I include in a class using Module#extend. That  
module defines an instance method which returns the name of the module  
that defined the method. Currently, I do that by hard-coding the string  
into the method definition, but it seems that there should really be a  
better way.

Here’s some code:

irb(main):001:0\> module Awesome  
irb(main):002:1\> def who\_am\_i  
irb(main):003:2\> ‘Awesome’ # Repeating myself, bad!  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> class One  
irb(main):007:1\> def initialize(what)  
irb(main):008:2\> extend what  
irb(main):009:2\> end  
irb(main):010:1\> end  
=\> nil  
irb(main):011:0\> a = One.new(Awesome)  
=\> #One:0x40322710  
irb(main):012:0\> a.who\_am\_i  
=\> “Awesome”

-Payton

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [December 29, 2005, 2:28am UTC](https://www.ruby-forum.com/t/get-mixins-name/51279/2 "2005-12-29T02:28:43Z")

</div>

On 12/28/05, Payton S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A somewhat weird situation.
> 
> I have a module which I include in a class using Module#extend. That  
> module defines an instance method which returns the name of the module  
> that defined the method. Currently, I do that by hard-coding the string  
> into the method definition, but it seems that there should really be a  
> better way.

The string is simple though… but if you want another way you could  
look at Module#ancestors on the singleton class (since you use  
extend).

module A end  
a = “some\_object”.extend A #=\> “abc”  
singleton\_class = class \<\< a; self end # #\<Class:#String:...\>  
singleton\_class.ancestors #=\> [A, String, Enumerable, Comparable,  
Object, Kernel]  
singleton\_class.ancestors.first #=\> A

# If you want it in string form:

singleton\_class.ancestors.first.to\_s #=\> “A”

You would want to wrap this up in some sort of method (at least the  
class \<\< part).

Brian.

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [December 29, 2005, 3:04am UTC](https://www.ruby-forum.com/t/get-mixins-name/51279/3 "2005-12-29T03:04:54Z")

</div>

Hi –

On Thu, 29 Dec 2005, Payton S. wrote:

> irb(main):002:1\> def who\_am\_i  
> irb(main):011:0\> a = One.new(Awesome)  
> =\> #One:0x40322710  
> irb(main):012:0\> a.who\_am\_i  
> =\> “Awesome”

Does this help?

module Awesome  
s = self  
define\_method(“who\_am\_i”) { s }  
end

obj = Object.new  
obj.extend(Awesome)  
p obj.who\_am\_i # Awesome

There’s a bit of semantic weirdness in the object claiming to be the  
module 🙂 But anyway, that’s one way to get the module object into  
one of its own instance methods.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [December 29, 2005, 11:58am UTC](https://www.ruby-forum.com/t/get-mixins-name/51279/4 "2005-12-29T11:58:59Z")

</div>

Payton S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):001:0\> module Awesome  
> =\> nil  
> irb(main):011:0\> a = One.new(Awesome)  
> =\> #One:0x40322710  
> irb(main):012:0\> a.who\_am\_i  
> =\> “Awesome”
> 
> -Payton

If you want to find out the defining class of a method you can use  
#method:

> > module Awesome; def foo() end end  
> > =\> nil  
> > class One; include Awesome; end  
> > =\> One  
> > One.new.method(:foo).to\_s  
> > =\> “#\<Method: One(Awesome)#foo\>”

Btw, what do you need that for?

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [December 29, 2005, 5:26pm UTC](https://www.ruby-forum.com/t/get-mixins-name/51279/5 "2005-12-29T17:26:09Z")

</div>

The goal was basically this for a GUI (the answer is probably that  
there’s a better way):

module Layout

# … layout stuff

def layout\_name  
‘none’  
end  
end

module CoolLayout  
include Layout

# … override some layout stuff.

def layout\_name  
‘CoolLayout’ # Not DRY  
end  
end

class Window  
def layout=(l)  
extend l  
end  
end

a = Window.new  
a.layout = CoolLayout  
puts “This window has the layout #{a.layout\_name}”

-Payton

---

<div class="post-metadata">

**Author:** ![Payton\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/74df32/32.png) [@Payton\_S](https://www.ruby-forum.com/u/Payton_S)\
**Post date:** [December 31, 2005, 12:09pm UTC](https://www.ruby-forum.com/t/get-mixins-name/51279/6 "2005-12-31T12:09:04Z")

</div>

Payton S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> module CoolLayout  
> end  
> end

IMHO this is a bad idea because you cannot change layout that way  
multiple  
times. Once you extended an instance by a module this module stays in  
place  
even if you extend with a different module. Also, a Window _has_ a  
layout -  
so composition is far more appropriate here than inheritance. And if  
you do  
that and model layouts as independent class hierarchy you can simply use  
the  
layout instance’s class or it’s name for the layout name.

> a = Window.new  
> a.layout = CoolLayout  
> puts “This window has the layout #{a.layout\_name}”

I’d prefer to let the layout answer the question after its name which  
also  
nicely fits with the composition approach - and it helps keep the  
interface  
of Window focused:

puts “This window has the layout #{a.layout.name}”

Kind regards

```
robert

```
