开发者

ways to define a global method in ruby

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开发者_JAVA百科 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?


If you define private method in Kernel module it will be available in the whole project. You will also rewrite desc 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 in Rake or RSpec.
P.S. Making methods private prevents other moludes or classes (but not subclasses) to use them (but not owerwrite) - I mean module nesting hierarchy.


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 omitting self has usually the same effect.

class Xyzzy
  private
  def foo
    puts "Foo called"
  end

  public
  def do_it
    foo       # <= Is OK
    self.foo  # <= raises NoMethodError
  end
end

Xyzzy.new.do_it

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 extending Kernel 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:

== Version 0.9.0

  • Incompatible *change*: Rake DSL commands ('task', 'file', etc.) are no longer private methods in Object. If you need to call 'task :xzy' inside your class, include Rake::DSL into the class. The DSL is still available at the top level scope (via the top level object which extends Rake::DSL).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜