Module: OOP::Concepts::FinalMethod

Includes:
FinalMethodVersion
Included in:
Object
Defined in:
lib/final_method/final_method.rb

Overview

‘method_finalizer’ is a gem that allows you to declare final class and instance methods in your ruby program.

Using ‘method_finalizer’

Basics

Require ‘rubygems’ and ‘method_finalizer’ gems.

require 'rubygems'
require 'method_finalizer'

class A
  # declare your final class and instance methods
  final_class_method :method_one
  final_instance_methods :method_two, :method_three

  def A.method_one
     "'method_one' class method in A declared as final"
  end

  def method_two
    "'method_two' instance method in A declared as final"
  end

  def method_three
    "'method_three' instance method in A declared as final"
  end
end

Now,

class B < A
  # attempting to redeclare 'method_one' as final class method
  final_class_method :method_one
end

will yield this error

FinalClassMethodRedeclarationError: cannot declare 'method_one' as final method in child B as parent A declared it already as such

and when,

class B < A
  # attempting to redefine 'method_one'
  def self.method_one
    "'method_one' class method in B"
  end
end

will give you

FinalClassMethodRedefinitionError: cannot redefine 'method_one' because it is already defined as final class method in parent A.

Constant Summary

Constants included from FinalMethodVersion

OOP::Concepts::FinalMethodVersion::FINAL_METHOD_VERSION

Instance Method Summary collapse

Instance Method Details

#final_class_methods(*method_names) ⇒ Object Also known as: final_class_method, final_singleton_methods, final_singleton_method

This will set a class method as final.

class A
  final_class_method :method_one
  def self.method_one
    "final class method 'method_one'"
  end
end

Attempting to redeclare it as final class method in subclass,

class B < A
  final_class_method :method_one
end

will give you

FinalClassMethodRedeclarationError: cannot declare 'method_one' as final method in child B as parent A declared it already as such

and when,

class B < A
  def B.method_one
    "'method_one' class method in B"
  end
end

will give you

FinalClassMethodRedefinitionError: cannot redefine 'method_one' because it is already defined as final class method in parent A.


96
97
98
# File 'lib/final_method/final_method.rb', line 96

def final_class_methods(*method_names)
  final_methods(@@final_singleton_methods, method_names, 'class')
end

#final_instance_methods(*method_names) ⇒ Object Also known as: final_instance_method

This will set an instance method as final.

class A
  final_instance_method :method_two
  def method_two
    "final instance method 'method_two'"
  end
end

Attempting to redeclare it as final instance method in subclass,

class B < A
  final_instance_method :method_two
end

will give you

FinalInstanceMethodRedeclarationError: cannot declare 'method_two' as final method in child B as parent A declared it already as such

and when,

class B < A
  def method_one
    "'method_one' class method in B"
  end
end

will give you

FinalInstanceMethodRedefinitionError: cannot redefine 'method_one' because it is already defined as final instance method in parent A.


178
179
180
# File 'lib/final_method/final_method.rb', line 178

def final_instance_methods(*method_names)
  final_methods(@@final_instance_methods, method_names, 'instance')
end

#get_final_instance_methods(sorted = false) ⇒ Object

Returns all final instance methods. Accepts optional parameter for sorting.

class A
  final_instance_methods :method_one, :method_two
  def method_one
    # any code goes here
  end
  def method_two
    # any code goes here again
  end
end

class B < A
  final_instance_method :method_three
  def method_three
  end
end

B.get_final_instance_methods

will give you

[:method_one, :method_two, :method_three] # order is random

if you will provide sort parameter, like

B.get_final_instance_methods(true) # sort is true

you will get

[:method_one, :method_three, :method_two]


222
223
224
# File 'lib/final_method/final_method.rb', line 222

def get_final_instance_methods(sorted=false)
  get_final_methods(@@final_instance_methods, sorted)
end

#get_final_singleton_methods(sorted = false) ⇒ Object Also known as: get_final_class_methods

Returns all final singleton (class) methods. Accepts optional parameter for sorting.

class A
  final_singleton_methods :method_one, :method_two
  def self.method_one
    # any code goes here
  end
  def self.method_two
    # any code goes here again
  end
end

class B < A
  final_class_method :method_three
  def self.method_three
  end
end

B.get_final_singleton_methods

will give you

[:method_one, :method_two, :method_three] # order is random

if you will provide sort parameter, like

B.get_final_singleton_methods(true) # sort is true

you will get

[:method_one, :method_three, :method_two]


136
137
138
# File 'lib/final_method/final_method.rb', line 136

def get_final_singleton_methods(sorted=false)
  get_final_methods(@@final_singleton_methods, sorted)
end

#method_added(method_name) ⇒ Object

:stopdoc:



184
185
186
# File 'lib/final_method/final_method.rb', line 184

def method_added(method_name)
  __method_added__(@@final_instance_methods, method_name, 'instance')
end

#singleton_method_added(method_name) ⇒ Object

:stopdoc:



142
143
144
# File 'lib/final_method/final_method.rb', line 142

def singleton_method_added(method_name)
  __method_added__(@@final_singleton_methods, method_name, 'class')
end