Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ways to define a global method in ruby

Tags:

ruby

global

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?

like image 814
Tao Avatar asked Aug 25 '11 09:08

Tao


People also ask

How do you define a global variable in Ruby?

Global Variable has global scope and accessible from anywhere in the program. Assigning to global variables from any point in the program has global implications. Global variable are always prefixed with a dollar sign ($).

How many types of methods are there in Ruby?

In Ruby classes there are two main types of methods: class and instance. These methods perform different tasks.

How do you define a method within a class using Ruby?

Class Methods are the methods that are defined inside the class, public class methods can be accessed with the help of objects. The method is marked as private by default, when a method is defined outside of the class definition. By default, methods are marked as public which is defined in the class definition.

How do you create a method in Ruby?

Defining & Calling the method: In Ruby, the method defines with the help of def keyword followed by method_name and end with end keyword. A method must be defined before calling and the name of the method should be in lowercase. Methods are simply called by its name.


2 Answers

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.

like image 180
bor1s Avatar answered Sep 22 '22 12:09

bor1s


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).
like image 21
Arsen7 Avatar answered Sep 18 '22 12:09

Arsen7