Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using class << self, when to use classes or modules?

Is there a difference in usage between

class Helper
  class << self
    # ...
  end
end

and

module Helper
  class << self
    # ...
  end
end

When would you use one over the other?

like image 366
Andres Riofrio Avatar asked Apr 05 '12 22:04

Andres Riofrio


People also ask

What does class << self do?

In the above example, class << self modifies self so it points to the metaclass of the Zabuton class. When a method is defined without an explicit receiver (the class/object on which the method will be defined), it is implicitly defined within the current scope, that is, the current value of self.

When should I use a module?

A module can be used either as a way to mix functionality into multiple classes, or as a way to provide one-off features that don't need to be instantiated or to keep track of state. A class method could also be used for the latter.

What is self in a Ruby module?

To answer your question, you can use self to define your method as a class method inside your module, and without self to define the method as instance method and it depends on your need and usecase that when you want what type of behaviour from your module methods. Follow this answer to receive notifications.

What is the difference between module and class in Rails?

What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).


2 Answers

The class<<self seems to be a red herring, as the only difference here is a class versus a module. Perhaps you're asking "I want to create an object that I do not intend to instantiate, but which exists only as a namespace for some methods (and possibly as a singleton with its own, global, state)."

If this is the case, both will function equally well. If there is any chance that you might want to create a derivative (another object inheriting the same methods) then you should use a class as it slightly is easier to write:

class Variation < Helper

instead of

module Helper
  module OwnMethods
    # Put methods here instead of class << self
  end
  extend OwnMethods
end

module Variation
  extend Helper::OwnMethods

However, for just namespacing I would generally use a module over a class, as a class implies that instantiation will occur.

like image 70
Phrogz Avatar answered Oct 03 '22 11:10

Phrogz


The difference between a Module and a Class is that you can make an instance of a Class, but not a Module. If you need to create an instance of Helper (h = Helper.new) then it should be a class. If not, it is probably best to remain a module. I'm not sure how the rest of your code is relevant to the question; whether you have class methods on a Module or a Class is not relevant to whether you need to create instances of that object.

like image 22
Myrddin Emrys Avatar answered Oct 03 '22 10:10

Myrddin Emrys