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.
98 99 100 101 |
# File 'lib/final_method/final_method.rb', line 98 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
235 236 237 |
# File 'lib/final_method/final_method.rb', line 235 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.
186 187 188 189 |
# File 'lib/final_method/final_method.rb', line 186 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?
144 145 146 |
# File 'lib/final_method/final_method.rb', line 144 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]
231 232 233 |
# File 'lib/final_method/final_method.rb', line 231 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]
139 140 141 |
# File 'lib/final_method/final_method.rb', line 139 def get_final_singleton_methods(sorted=false) get_final_methods(@@final_singleton_methods, sorted) end |
#method_added(method_name) ⇒ Object
:stopdoc:
193 194 195 |
# File 'lib/final_method/final_method.rb', line 193 def method_added(method_name) __method_added__(@@final_instance_methods, method_name, 'instance') end |
#singleton_method_added(method_name) ⇒ Object
:stopdoc:
150 151 152 |
# File 'lib/final_method/final_method.rb', line 150 def singleton_method_added(method_name) __method_added__(@@final_singleton_methods, method_name, 'class') end |