[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Adwin Wijaya

6/29/2007 6:02:00 AM

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 !

--
Posted via http://www.ruby-....

3 Answers

Jano Svitok

6/29/2007 8:06:00 AM

0

On 6/29/07, Adwin Wijaya <adwin.wijaya@gmail.com> wrote:
> 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 !

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.

Adwin Wijaya

6/29/2007 10:40:00 AM

0

Jano Svitok 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 ?

^_^:

--
Posted via http://www.ruby-....

Jano Svitok

6/29/2007 11:11:00 AM

0

On 6/29/07, Adwin Wijaya <adwin.wijaya@gmail.com> 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/pag...

> 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-mo...
http://www.rubyfleebie.com/diving-into-ruby-object-mo...
http://rubyforge.org/docman/view.php/251/96/ChrisPin...

J.