# Modules and Class --- what is that ? --

**URL:** <https://www.ruby-forum.com/t/modules-and-class-what-is-that/108129>\
**Category:** Ruby\
**Created:** [June 29, 2007, 8:02am UTC](https://www.ruby-forum.com/t/modules-and-class-what-is-that/108129 "2007-06-29T08:02:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nightingale2k1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nightingale2k1](https://www.ruby-forum.com/u/nightingale2k1)\
**Post date:** [June 29, 2007, 8:02am UTC](https://www.ruby-forum.com/t/modules-and-class-what-is-that/108129/1 "2007-06-29T08:02:07Z")

</div>

Hi …

I am newb in Ruby world. I am get used with PHP and Java before,  
therefore I got confused when I learn ruby syntax  
I have a question what is the difference between Modules and Class ?

Also, how to know all methods that accessible from ActiveRecord::Base  
and ActiveRecord::Migration ? I read the API documentation, but I  
couldnt find ActiveRecord::Base has validation methods  
(validates\_presence\_of, validate, etc) but I found it on somewhere else  
Class/Modules.  
It also happen with Migration, I couldnt found it.

I read from the source (.rb) as well … and I have no clue …  
Thank you !

---

<div class="post-metadata">

**Author:** ![nightingale2k1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nightingale2k1](https://www.ruby-forum.com/u/nightingale2k1)\
**Post date:** [June 29, 2007, 10:06am UTC](https://www.ruby-forum.com/t/modules-and-class-what-is-that/108129/2 "2007-06-29T10:06:49Z")

</div>

On 6/29/07, Adwin W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Class/Modules.  
> It also happen with Migration, I couldnt found it.
> 
> I read from the source (.rb) as well … and I have no clue …  
> Thank you !

Module is a (partial) equivalent to Java’s Interface. It can contain  
constants and methods (and, unlike Interfaces, method implementation  
as well). You cannot derive from it, so it’s kind of dead end. You can  
however, include one module into another.

So they are kind of building bricks from which you can compose your  
class’ behaviour.

Now to the methods:

There are several kinds of methods: instance methods, class methods  
and singleton class methods. (They are in fact the same, but they  
differ to what object they belong and how you invoke them).

These validation methods are class methods of the ActiveRecord::Base,  
and you can find them in some ClassMethods module somewhere in the  
ActiveRecord sources [1]. This module is then included into AR::Base  
class.

Many of the “magic” methods are implemented using method\_missing, that  
is called whenever a method is not found, before raising exception  
(e.g. find\_by\_…)

To see the what methods a class has, startup rails console [2], and  
issue ActiveRecord::Base.methods.sort (for class methods)

and ActiveRecord::Base.instance\_methods.sort (for instance methods)

I will leave up to you to lookup the details of [1] and [2] 😉

J.

---

<div class="post-metadata">

**Author:** ![nightingale2k1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nightingale2k1](https://www.ruby-forum.com/u/nightingale2k1)\
**Post date:** [June 29, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/modules-and-class-what-is-that/108129/3 "2007-06-29T12:22:03Z")

</div>

I am new to Ruby too. propably newer than any one else… I got a list of  
site that I found helpful you may know them already but here they are  
anyway.  
[http://poignantguide.net/ruby](http://poignantguide.net/ruby)  
[http://pine.fm/LearnToProgram](http://pine.fm/LearnToProgram)

hope this proves useful…

---

<div class="post-metadata">

**Author:** ![nightingale2k1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nightingale2k1](https://www.ruby-forum.com/u/nightingale2k1)\
**Post date:** [June 29, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/modules-and-class-what-is-that/108129/4 "2007-06-29T12:39:48Z")

</div>

Jano S. wrote:

> Many of the “magic” methods are implemented using method\_missing, that  
> is called whenever a method is not found, before raising exception  
> (e.g. find\_by\_…)
> 
> To see the what methods a class has, startup rails console [2], and  
> issue ActiveRecord::Base.methods.sort (for class methods)
> 
> and ActiveRecord::Base.instance\_methods.sort (for instance methods)
> 
> I will leave up to you to lookup the details of [1] and [2] 😉
> 
> J.

Thank you Jano,  
my last question is, how to startup rails console, so that i could see  
the methods for class and instance methods …

What is the difference between instance methods and class methods ?

^\_^:

---

<div class="post-metadata">

**Author:** ![nightingale2k1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nightingale2k1](https://www.ruby-forum.com/u/nightingale2k1)\
**Post date:** [June 29, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/modules-and-class-what-is-that/108129/5 "2007-06-29T13:13:10Z")

</div>

On 6/29/07, Adwin W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you Jano,  
> my last question is, how to startup rails console, so that i could see  
> the methods for class and instance methods …

[http://wiki.rubyonrails.org/rails/pages/Console](http://wiki.rubyonrails.org/rails/pages/Console)

> What is the difference between instance methods and class methods ?

instance methods are methods of the object itself. class methods are  
methods of the object’s class (each object has its class, and that  
class is in turn again an object, os it has its owm methods).

for more info try this:  
[http://www.rubyfleebie.com/diving-into-ruby-object-model-part-1](http://www.rubyfleebie.com/diving-into-ruby-object-model-part-1)  
[http://www.rubyfleebie.com/diving-into-ruby-object-model-part-2](http://www.rubyfleebie.com/diving-into-ruby-object-model-part-2)  
[http://rubyforge.org/docman/view.php/251/96/ChrisPine\_UROM.ppt](http://rubyforge.org/docman/view.php/251/96/ChrisPine_UROM.ppt)

J.
