What is `module_function` all about?
I saw this in an open source project (pagy):
module Car
def drive
puts "\nI'm a racing car passing by
\nLike Lady Godiva
\nI'm gonna go, go, go
\nThere's no stopping me"
end
end
class Toyota
include Car
end
What do I have to do to the Car module in order to make this possible.
Car.drive
The easy answer is to add in the module_function method.
module Car
module_function
def drive
puts "\nI'm a racing car passing by
\nLike Lady Godiva
\nI'm gonna go, go, go
\nThere's no stopping me"
end
end
class Toyota
include Car
end
Car.drive # this should work now.
For more info, check out the documentation on module_function.
Written on April 10, 2026