Created
April 9, 2025 03:19
-
-
Save aks/b11b5cd4a33e6aba8e55f24cdabdf1bf to your computer and use it in GitHub Desktop.
Small ruby example to show that module methods are private by default
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
$ ./module_methods_are_private.rb | |
one | |
two | |
three | |
four | |
Main public methods: [:three, :four, :now] | |
Main private methods: [] | |
ModuleMethods public methods: [] | |
ModuleMethods private methods: [:append_features, :extend_object, :prepend_features, :refine, :module_function] |
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
#!/usr/bin/env ruby | |
module ModuleMethods | |
public def one | |
puts 'one' | |
end | |
private | |
def two | |
puts 'two' | |
end | |
end | |
class Main | |
def now | |
one | |
two | |
three | |
four | |
puts "Main public methods: #{Main.new.public_methods(false)}" | |
puts "Main private methods: #{Main.new.private_methods(false)}" | |
puts "ModuleMethods public methods: #{ModuleMethods.public_methods(false) - Module.public_methods}" | |
puts "ModuleMethods private methods: #{ModuleMethods.private_methods(false) - Module.private_methods}" | |
end | |
def three | |
puts 'three' | |
end | |
include ModuleMethods | |
def four | |
puts 'four' | |
end | |
end | |
Main.new.now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment