Skip to content

Commit

Permalink
Adding page on modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jan 4, 2008
1 parent f1a3113 commit 793c7b1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions site/src/layouts/application.haml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
=link '<tt>include</tt> and <tt>extend</tt>', '/includeextend.html'
%li
=link 'Callbacks and hooks', '/hooks.html'
%li
=link 'Modules', '/modules.html'
%li
=link 'Type checking and the class tree', '/types.html'
#footer
Expand Down
41 changes: 41 additions & 0 deletions site/src/pages/modules.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
:textile
h3. Modules

Ruby modules are collections of methods that can be mixed into classes. The instance methods
in a module cannot be accessed from outside it, they can only be used in classes that
@include@ the module.

@JS.Class@ has some functionality to mimick modules. @JS.Module@ creates objects that have
one method - an @included@ hook - that can be mixed into classes but cannot have their
instance methods modified. This allows you to create secure mixins.

<pre>
var Comparable = new JS.Module({
extend: {
compare: function(a,b) {
return a.compareWith(b);
}
},

lt: function(object) {
return this.klass.compare(this, object) == -1;
},
lte: function(object) {
return this.klass.compare(this, object) < 1;
},
gt: function(object) {
return this.klass.compare(this, object) == 1;
},
gte: function(object) {
return this.klass.compare(this, object) > -1;
},
eq: function(object) {
return this.klass.compare(this, object) == 0;
}
});</pre>

@Comparable@'s instance methods are now not accessible unless the module is mixed into a class:

<pre>
TodoItem.include(Comparable);
// Now TodoItem has all the methods defined above</pre>

0 comments on commit 793c7b1

Please sign in to comment.