Module: OOP::Concepts::FinalMethod
Overview
‘methodfinalizer’ is a gem that allows you to declare final class and instance methods in your ruby program.
Using ‘methodfinalizer’
Basics
Require ‘rubygems’ and ‘methodfinalizer’ gems.
require 'rubygems'
require 'methodfinalizer'
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
-
#final_class_methods(*method_names) ⇒ Object
(also: #final_class_method, #final_singleton_methods, #final_singleton_method)
This will set a class method as final.
- #final_instance_method_exists?(method) ⇒ Boolean
-
#final_instance_methods(*method_names) ⇒ Object
(also: #final_instance_method)
This will set an instance method as final.
- #final_singleton_method_exists?(method) ⇒ Boolean (also: #final_class_method_exists?)
-
#get_final_instance_methods(sorted = false) ⇒ Object
Returns all final instance methods.
-
#get_final_singleton_methods(sorted = false) ⇒ Object
(also: #get_final_class_methods)
Returns all final singleton (class) methods.
-
#method_added(method_name) ⇒ Object
:stopdoc:.
-
#singleton_method_added(method_name) ⇒ Object
:stopdoc:.
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 99 |
# File 'lib/final_method/final_method.rb', line 96 def final_class_methods(*method_names) raise_error_when_method_names_empty_or_nil(method_names) final_methods(@@final_singleton_methods, method_names, 'class') end |
#final_instance_method_exists?(method) ⇒ Boolean
233 234 235 |
# File 'lib/final_method/final_method.rb', line 233 def final_instance_method_exists?(method) final_method_exists?(@@final_instance_methods, method) 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.
184 185 186 187 |
# File 'lib/final_method/final_method.rb', line 184 def final_instance_methods(*method_names) raise_error_when_method_names_empty_or_nil(method_names) final_methods(@@final_instance_methods, method_names, 'instance') end |
#final_singleton_method_exists?(method) ⇒ Boolean Also known as: final_class_method_exists?
142 143 144 |
# File 'lib/final_method/final_method.rb', line 142 def final_singleton_method_exists?(method) final_method_exists?(@@final_singleton_methods, method) 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]
229 230 231 |
# File 'lib/final_method/final_method.rb', line 229 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]
137 138 139 |
# File 'lib/final_method/final_method.rb', line 137 def get_final_singleton_methods(sorted=false) get_final_methods(@@final_singleton_methods, sorted) end |
#method_added(method_name) ⇒ Object
:stopdoc:
191 192 193 |
# File 'lib/final_method/final_method.rb', line 191 def method_added(method_name) __method_added__(@@final_instance_methods, method_name, 'instance') end |
#singleton_method_added(method_name) ⇒ Object
:stopdoc:
148 149 150 |
# File 'lib/final_method/final_method.rb', line 148 def singleton_method_added(method_name) __method_added__(@@final_singleton_methods, method_name, 'class') end |