Skip to content

Instantly share code, notes, and snippets.

@aks
Created April 9, 2025 03:19
Show Gist options
  • Save aks/b11b5cd4a33e6aba8e55f24cdabdf1bf to your computer and use it in GitHub Desktop.
Save aks/b11b5cd4a33e6aba8e55f24cdabdf1bf to your computer and use it in GitHub Desktop.
Small ruby example to show that module methods are private by default
$ ./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]
#!/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