RSS Amplifier

Ben Koshy · Apr 10, 2026

What is `module_function` all about?

0
Sign in to vote or save

This page did not load. You can still read it on the original site — the toolbar below keeps your place in the directory.

I saw this in an open source project (pagy):

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.

Read on tek1.com.au

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.