# Ruby modules and classes

**URL:** <https://www.ruby-forum.com/t/ruby-modules-and-classes/75232>\
**Category:** Ruby\
**Created:** [March 1, 2006, 3:58pm UTC](https://www.ruby-forum.com/t/ruby-modules-and-classes/75232 "2006-03-01T15:58:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 1, 2006, 3:58pm UTC](https://www.ruby-forum.com/t/ruby-modules-and-classes/75232/1 "2006-03-01T15:58:09Z")

</div>

Hello,

When I try the code below I get an error. Why is it so? Am I  
misunderstanding  
how modules behave? How should the module be written so that `who\_am\_i’  
is  
added as a method to the class Phonograph.

module Debug  
def self.who\_am\_i?  
“#{self.class.name} (##{self.id}): #{self.to\_s}”  
end  
end  
class Phonograph  
include Debug  
end  
Phonograph.who\_am\_i?

Thanks.

–  
Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| “O thou my friend! The prosperity of Crime is like unto the lightning,  
| whose traitorous brilliancies embellish the atmosphere but for an  
| instant, in order to hurl into death’s very depths the luckless one  
| they have dazzled.” – Marquis de Sade  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 1, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/ruby-modules-and-classes/75232/2 "2006-03-01T16:12:38Z")

</div>

On Wed, 2006-03-01 at 23:57 +0900, Surendra S. wrote:

> end  
> class Phonograph  
> include Debug  
> end  
> Phonograph.who\_am\_i?

Depends what you want, but how about:

```
module Debug
  def who_am_i?
    "#{self.class.name} (\##{self.object_id}): #{self.to_s}"
  end
end

class Phonograph
  class << self
    include Debug
  end
end

p Phonograph.who_am_i?

```

Or, if you want it as both class and instance methods, I like this  
trick:

```
module Debug
  def who_am_i?
    "#{self.class.name} (\##{self.object_id}): #{self.to_s}"
  end

  def self.included(othermod)
    othermod.extend(self)
  end
end

class Phonograph
  include Debug
end

p Phonograph.who_am_i?
p Phonograph.new.who_am_i?

```

(P.s. do you know about Object#inspect?)

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [March 2, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/ruby-modules-and-classes/75232/3 "2006-03-02T14:16:30Z")

</div>

Hello Ross,

Ross B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > end  
> > end  
> > class Phonograph  
> > include Debug  
> > end  
> > Phonograph.who\_am\_i?
> 
> Depends what you want, but how about:

Thanks for your reply.

> end
> 
> p Phonograph.who\_am\_i?

In the meantime I found another way to do this:

class Phonograph  
extend Debug  
end

are the two methods equivalent, or is there any difference?

> ```
> end
> 
> ```
> 
> end
> 
> class Phonograph  
> include Debug  
> end
> 
> p Phonograph.who\_am\_i?  
> p Phonograph.new.who\_am\_i?

> (P.s. do you know about Object#inspect?)  
> Now I do.

Thanks once again, I appreciate your help.

–  
Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read the latest news at: [http://news.kreeti.com](http://news.kreeti.com)  
,----  
| Great wits are sure to madness near allied,  
| And thin partitions do their bounds divide.  
|  
| (John Dryden, Absalom and Achitophel, 1681)  
`----
