You must use the Kernel
module, which is included with Object
. It is where private methods such as exit
, puts
and require
are defined, so it is a great choice for defining an imperative API.
When you extend Object
, people expect to be able to explicitly call your methods on any object, and also understand that your method depends on this state of the object.
Kernel
methods are understood differently. Even if they are technically available for all objects, you do not expect people to write things like:
'some string'.sleep 1000
It does not make sense. sleep
has nothing to do with a string; it in no way depends on it. It should only be called with an implicit receiver, as if the concept of self
itself did not exist.
Creating your private methods and extending Kernel
instead will help you get this message.
You can do this in foo.rb
:
module Foo
When you load
or require
some file, it runs line by line. You can put arbitrary code anywhere in the file, even inside module and class definitions. You can use this to set up your library correctly so that others do not need it.
source share