This demonstrates the difference in behaviour when using the public|protected|private_methods
methods to check for the presence of a module method defined on Kernel
or Object
versus those defined in another Module.
Last active
January 26, 2017 20:25
-
-
Save chrisroos/12a1b032b95664448c9e987132f33988 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def method_in_object | |
'method-in-object' | |
end | |
end | |
module ModuleWithMethod | |
def method_in_module | |
'method-in-module' | |
end | |
end | |
module Mod | |
extend ModuleWithMethod | |
end | |
puts '# Calling module methods' | |
puts "Mod.method_in_object = #{Mod.method_in_object.inspect}" | |
puts "Mod.method_in_module = #{Mod.method_in_module.inspect}" | |
puts | |
puts '# Checking existence of Mod.method_in_object' | |
puts "Mod.public_methods(false) = #{Mod.public_methods(false).include?(:method_in_object)}" | |
puts "Mod.protected_methods(false) = #{Mod.protected_methods(false).include?(:method_in_object)}" | |
puts "Mod.private_methods(false) = #{Mod.private_methods(false).include?(:method_in_object)}" | |
puts "Mod.public_methods(true) = #{Mod.public_methods(true).include?(:method_in_object)}" | |
puts "Mod.protected_methods(true) = #{Mod.protected_methods(true).include?(:method_in_object)}" | |
puts "Mod.private_methods(true) = #{Mod.private_methods(true).include?(:method_in_object)}" | |
puts | |
puts '# Checking existence of Mod.method_in_module' | |
puts "Mod.public_methods(false) = #{Mod.public_methods(false).include?(:method_in_module)}" | |
puts "Mod.protected_methods(false) = #{Mod.protected_methods(false).include?(:method_in_module)}" | |
puts "Mod.private_methods(false) = #{Mod.private_methods(false).include?(:method_in_module)}" | |
puts "Mod.public_methods(true) = #{Mod.public_methods(true).include?(:method_in_module)}" | |
puts "Mod.protected_methods(true) = #{Mod.protected_methods(true).include?(:method_in_module)}" | |
puts "Mod.private_methods(true) = #{Mod.private_methods(true).include?(:method_in_module)}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calling module methods | |
Mod.method_in_object = "method-in-object" | |
Mod.method_in_module = "method-in-module" | |
# Checking existence of Mod.method_in_object | |
Mod.public_methods(false) = false | |
Mod.protected_methods(false) = false | |
Mod.private_methods(false) = false | |
Mod.public_methods(true) = true | |
Mod.protected_methods(true) = false | |
Mod.private_methods(true) = false | |
# Checking existence of Mod.method_in_module | |
Mod.public_methods(false) = true | |
Mod.protected_methods(false) = false | |
Mod.private_methods(false) = false | |
Mod.public_methods(true) = true | |
Mod.protected_methods(true) = false | |
Mod.private_methods(true) = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment