module Final

Make your class final. Once final'ized, it cannot be subclassed and its methods cannot be redefined.

Constants

FINAL_VERSION

The version of the final library.

Public Class Methods

included(mod) click to toggle source
# File lib/final.rb, line 11
def self.included(mod)
  mod.instance_eval do
    # Store already defined methods.
    def final_methods
      @final_methods ||= []
    end

    # Prevent subclassing, except implicity subclassing from Object.
    def inherited(_sub)
      raise Error, "cannot subclass #{self}" unless self == Object
    end

    # Prevent methods from being redefined.
    #--
    # There's still going to be a method redefinition warning. Gosh, it
    # sure would be nice if we could disable warnings.
    #
    def method_added(sym)
      if final_methods.include?(sym)
        raise Error, "method '#{sym}' already defined"
      else
        final_methods << sym
      end
    end
  end
end

Public Instance Methods

final_methods() click to toggle source

Store already defined methods.

# File lib/final.rb, line 14
def final_methods
  @final_methods ||= []
end
inherited(_sub) click to toggle source

Prevent subclassing, except implicity subclassing from Object.

# File lib/final.rb, line 19
def inherited(_sub)
  raise Error, "cannot subclass #{self}" unless self == Object
end
method_added(sym) click to toggle source

Prevent methods from being redefined.

# File lib/final.rb, line 28
def method_added(sym)
  if final_methods.include?(sym)
    raise Error, "method '#{sym}' already defined"
  else
    final_methods << sym
  end
end