I'm writing a small gem, and I want to define a DSL-like method, pretty much the same as the desc
and task
methods in Rake.
Rake defines them as private methods in the Rake::DSL
module and then
self.extend Rake::DSL
to mix the module into the main object? (I'm a newbie and go ahead laugh if I'm wrong)
what are the benefits by doing so? is it because making these methods private can prevent any other objects to use them (that is, to prevent something like some_obj.desc
) ?
what if I define the methods in Kernel
module Kernel
private
include Rake::DSL
end
Is there any difference?
Just to extend the answer given by bor1s, about the private methods:
In ruby you have "private" and "protected" methods. What bor1s says, is correct when talking about "protected" methods. Declaring a method "private" additionally prevents other instances of the same class from using the method.
When you call a "private" method, you cannot use a dot in front of it - you cannot even use
self.
, even though using or omittingself
has usually the same effect.If you change 'private' to 'protected' in the code above, no error will be raised.
And about modules:
The final result of defining a method in
Kernel
and extendingKernel
with the method defined in some module is the same: in both cases the method is global.Using a module is just a little more elegant, as it groups your changes in one place, but I would say it's a matter of personal taste.
Usually you do not include methods in Kernel or Object (as it may be a little dangerous), but you include (or extend) a specific class or object which needs these methods, and in this case you need your methods grouped in a module.
Even Rake in version 0.9.0 stopped including the DSL commands in Object:
If you define private method in
Kernel
module it will be available in the whole project. You will also rewritedesc
method that project use to define rake task. But if you write your methods in your submodule and then extend it in superclass or some module - you can easily write any kind of DSL lang like you might saw inRake
orRSpec
.P.S. Making methods
private
prevents other moludes or classes (but not subclasses) to use them (but not owerwrite) - I mean module nesting hierarchy.