# Class Module

**URL:** <https://www.ruby-forum.com/t/class-module/139172>\
**Category:** Ruby\
**Created:** [May 31, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/class-module/139172 "2008-05-31T23:35:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/85e7bf/32.png) [@rayn](https://www.ruby-forum.com/u/rayn)\
**Post date:** [May 31, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/class-module/139172/1 "2008-05-31T23:35:37Z")

</div>

Hi, I’m still learning ruby and i’m trying, but I just can’t understand  
this result…

class Module  
@@docs = {}

# Invoked during class definitions

def doc(str)  
@@docs[self.name] = self.name + “:\n” + str.gsub(/^\s+/, ‘’)  
end

# invoked to get documentation

def Module::doc(aClass)  
# If we’re passed a class or module, convert to string  
# (’\<=’ for classes checks for same class or subtype)  
aClass = aClass.name if aClass.class \<= Module  
@@docs[aClass] || “No documentation for #{aClass}”  
end  
end  
class Example  
doc(“This is a sample documentation string”)

# … rest of class

end

puts Module::doc(Example)

Produces:

Example:  
This is a sample documentation string

But

class Module  
@@docs = {}

# Invoked during class definitions

def doc(str)  
@@docs[self.name] = self.name + “:\n” + str.gsub(/^\s+/, ‘’)  
end

# invoked to get documentation

def Module::doc(aClass)  
# If we’re passed a class or module, convert to string  
# (’\<=’ for classes checks for same class or subtype)  
aClass = aClass.name if aClass.class \<= Module  
@@docs[aClass] || “No documentation for #{aClass}”  
end  
end  
class Example\<Module  
doc(“This is a sample documentation string”)

# … rest of class

end

puts Module::doc(Example)

Produces:

No documentation for Example

Can anybody tell me why?

---

<div class="post-metadata">

**Author:** ![rayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/85e7bf/32.png) [@rayn](https://www.ruby-forum.com/u/rayn)\
**Post date:** [June 1, 2008, 12:00am UTC](https://www.ruby-forum.com/t/class-module/139172/2 "2008-06-01T00:00:49Z")

</div>

Hi –

On Sun, 1 Jun 2008, Wesley Silva wrote:

> def Module::doc(aClass)  
> end  
> But
> 
> # (’\<=’ for classes checks for same class or subtype)
> 
> puts Module::doc(Example)
> 
> Produces:
> 
> No documentation for Example
> 
> Can anybody tell me why?

When you do this:

class Example \< Module  
doc(“string”)  
end

the doc method you’re calling is Module.doc. The object Example has  
two doc methods in its method lookup path, and that’s the one it hits  
first.

If you change Module.doc to Module.show\_doc (or whatever), you’ll see  
the difference.

David

---

<div class="post-metadata">

**Author:** ![rayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/85e7bf/32.png) [@rayn](https://www.ruby-forum.com/u/rayn)\
**Post date:** [June 1, 2008, 1:26am UTC](https://www.ruby-forum.com/t/class-module/139172/3 "2008-06-01T01:26:04Z")

</div>

Thanks a lot David!

I would like to make another question please…🙂

If i just change Modules’s name for Father for example:

class Father  
@@docs = {}  
def doc(str)  
@@docs[self.name] = self.name + “:\n” + str.gsub(/^\s+/, ‘’)  
end  
def Father::show\_doc(aClass)  
aClass = aClass.name if aClass.class \<= Module  
@@docs[aClass] || “No documentation for #{aClass}”  
end  
end  
class Example\<Father  
doc(“This is a sample documentation string”)  
end

puts Father::show\_doc(Example)

Produces:

undefined method `doc’ for Example:Class (NoMethodError)

Can you tell why? 🙂

---

<div class="post-metadata">

**Author:** ![rayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/85e7bf/32.png) [@rayn](https://www.ruby-forum.com/u/rayn)\
**Post date:** [June 1, 2008, 2:17am UTC](https://www.ruby-forum.com/t/class-module/139172/4 "2008-06-01T02:17:14Z")

</div>

Hi –

On Sun, 1 Jun 2008, Wesley Silva wrote:

> end
> 
> Produces:
> 
> undefined method `doc’ for Example:Class (NoMethodError)
> 
> Can you tell why? 🙂

doc is an instance method of Father, and Example is not an instance of  
Father (it’s an instance of Class). So you can’t call doc on Example.

David

---

<div class="post-metadata">

**Author:** ![rayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/85e7bf/32.png) [@rayn](https://www.ruby-forum.com/u/rayn)\
**Post date:** [June 1, 2008, 11:35am UTC](https://www.ruby-forum.com/t/class-module/139172/5 "2008-06-01T11:35:37Z")

</div>

On 01.06.2008 02:16, David A. Black wrote:

> > class Father  
> > doc(“This is a sample documentation string”)  
> > doc is an instance method of Father, and Example is not an instance of  
> > Father (it’s an instance of Class). So you can’t call doc on Example.

Wesley, IMHO there is an easier as well as safer solution: safer,  
because a class’s name may be unset. Also, your solution keeps  
documentation of all classes in memory even if they are GC’ed (if that’s  
possible, I am not sure). So here’s what I’d do:

class Module  
def doc(str = nil)  
if str  
@doc = (name + “:\n” + str.strip).freeze  
else  
@doc  
end  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/85e7bf/32.png) [@rayn](https://www.ruby-forum.com/u/rayn)\
**Post date:** [June 2, 2008, 7:11am UTC](https://www.ruby-forum.com/t/class-module/139172/6 "2008-06-02T07:11:53Z")

</div>

On Sunday 01 June 2008 04:34:33 Robert K. wrote:

> Also, your solution keeps  
> documentation of all classes in memory even if they are GC’ed (if that’s  
> possible, I am not sure).

I hope it’s possbile to GC a class, otherwise Class#new can be a memory  
leak.
