Hi Guys
I am new to ruby, and one question here: what object should I wrap as
Module and what should I wrap as Class, any rules to fellow?
Thanks
/Jack
Hi Guys
I am new to ruby, and one question here: what object should I wrap as
Module and what should I wrap as Class, any rules to fellow?
Thanks
/Jack
2007/11/15, jack.tang [email protected]:
Hi Guys
I am new to ruby, and one question here: what object should I wrap as
Module and what should I wrap as Class, any rules to fellow?
I am not sure what you mean by “wrap”. You often use modules where
you would use multiple inheritance in other languages. Generally a
module is easier to reuse because Ruby supports only single
inheritance.
See here for example:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html
Cheers
robert
jack.tang wrote:
Hi Guys
I am new to ruby, and one question here: what object should I wrap as
Module and what should I wrap as Class, any rules to fellow?Thanks
/Jack
Classes are extended from Modules with the added “functionality” that
they can create instances of themselves(objects). I hope this makes it
more conceptually clear for you in terms of "wrap"ing…
If you want to associate a group of methods into a name space, then a
module will suffice. If on the other hand, you want to create
functionality where the methods mostly act upon contained state(instance
variables), then a class will suit your needs better.
ilan
jack.tang wrote:
Hi Guys
I am new to ruby, and one question here: what object should I wrap as
Module and what should I wrap as Class, any rules to fellow?Thanks
/Jack
Besides the reference given above, another good source is D.Black’s
“Ruby on Rails”; chapter 6, “Class/Module Design” is entirely dedicated
to answer your question.
Sometimes the same problem can be attacked with different combinations
of classes & modules; the examples (and the discussion, where the author
indicates his preference) given in the chapter are very interesting in
this regard.
On Nov 15, 10:38 am, “jack.tang” [email protected] wrote:
I am new to ruby, and one question here: what object should I wrap as
Module and what should I wrap as Class, any rules to fellow?
Hi Jack,
Modules in a certain sense are more than one thing, and that can make
them hard to figure out.
In one sense, a Module is a stripped down Class. And as Ilan pointed
out, Class is itself a subclass of Module, so Class can take Module’s
basic functionality and build upon it. You can instantiate a Class
but not a Module.
So that’s one way of understanding Modules. But the more practical
way of understanding Modules is in how they’re used. And they’re used
in two very distinct ways.
The first way you use Modules is to collect a set of instance
methods. Now, what use could this have if you can’t instantiate a
Module, you may ask. Well a Class can “include” a Module and that
drops those instance methods in the class. This is called a “mix-in”
module, and you can define the methods in one place and mix them into
a number of Classes. For example, the Comparable module is a mix-in
module. If you define the <=> operator in your class and then mix in
Comparable, it will create five more operators (<, <=, >, >=, and ==)
and one method (between?) all based on the <=> operator you defined.
So now you can easily use the comparison operators for your Invoice
class or your Employee class.
But then there is a second use of Modules, and that is to define a
name-space, a space where you can name things – Classes, methods, and
other Modules – without worrying that the names might conflict with
other names defined elsewhere in your software. If you’re familiar
with Java’s packages, then this use of Modules is similar to that.
When you define a method in a Module that you’re using as a name-
space, then you define it a class method. You can then call it by
prepending the Module’s name and two colons before the method name, as
in:
MyModule::my_method(1, 'a', 2.2)
And you can also Classes and other Modules within the Module. If
you’ve used Rails you’ve probably seen this taken to a high degree,
allowing it to subdivide all its functionality into nice units. For
example, there’s a module three deep – ActiveRecord::Acts::List .
I hope that’s helpful!
Eric
====
Interested in hands-on, on-site Ruby training? See http://LearnRuby.com
for information about a well-reviewed class.
In general I would recommend to use module if you ever want
functionality to be spread to multiple classes, i.e. audio conversion,
set of constants and so on.
Modules are also used as namespace shields:
module Foo
class Bar
end
end
Foo::Bar.new
Hi –
On Fri, 16 Nov 2007, Raul P. wrote:
“Ruby on Rails”; chapter 6, “Class/Module Design” is entirely dedicated
to answer your question.
It’s “Ruby for Rails” (not “on”). But thanks for the nod
David
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs