4.2 Wrapping Up Modules

The `extended` callback

Just like included which is a callback for include, there is a callback for extend and it is appropriately called extended.

We'll breeze through it using a simple example:

Example Code:

Output Window

Namespacing non-OO code using module level methods

Object Oriented programming is fundamentally about grouping data and its related behaviour together. We call the data the 'state' of the object and any logic related to the state as the 'behaviour' of the object.

It is also possible (but not advisable) to write non-object oriented code in Ruby by avoiding creation of objects and instead relying on independent methods. Ruby's Math module is an example of a collection of such methods. This is how you can define and use a similar module:

Example Code:

Output Window

A quick drill before we move on. In the following exercise you have to define a static method square in the module Math. It should obviously return the square of the number passed to it. Alright, jump right in:

Output Window

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2024 by Jasim A Basheer