On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Module
A Module is a collection of methods and constants. The methods in a module may be instance methods or module methods. Instance methods appear as methods in a class when the module is included, module methods do not. Conversely, module methods may be called without creating an encapsulating object, while instance methods may not. (See Module#module_function.)
In the descriptions that follow, the parameter sym refers to a symbol, which is either a quoted string or a Symbol (such as :name).
module Mod include Math CONST = 1 def meth # ... end end Mod.class #=> Module Mod.constants #=> [:CONST, :PI, :E] Mod.instance_methods #=> [:meth]
Class methods 5
Instance methods 80
Ruby version changes 16
-
4.1
-
Kernel#autoload_relativeandModule#autoload_relativeare added. These methods work likeautoload, but resolve the file path relative to the file where the method is called, similar torequire_relative. This makes it easier to autoload constants from files in the same directory without hardcoding absolute paths or manipulating$LOAD_PATH. [Feature #15330] -
Module#descendantsis added. It returns an array of classes and modules that have the receiver in their ancestors. [Feature #9779] -
Module#ruby2_keywordsand top-levelruby2_keywordsare deprecated and will be removed in Ruby 4.4. [Feature #22205] -
Module#method_defined?now accepts a third optional argument to also match private methods. [Feature #22297]
-
-
3.3
-
Module#set_temporary_nameadded for setting a temporary name for a module. [Feature #19521]
-
-
3.2
-
Module.used_refinementshas been added. [Feature #14332] -
Module#refinementshas been added. [Feature #12737] -
Module#const_addedhas been added. [Feature #17881] -
Module#undefined_instance_methodshas been added. [Feature #12655]
-
-
3.1
-
Module#prependnow modifies the ancestor chain if the receiver already includes the argument.Module#prependstill does not modify the ancestor chain if the receiver has already prepended the argument. [Bug #17423] -
Module#private, public, protected, and module_function will now return their arguments. If a single argument is given, it is returned. If no arguments are given, nil is returned. If multiple arguments are given, they are returned as an array. [Feature #12495] -
New class which represents a module created by
Module#refine.includeandprependare deprecated, andimport_methodsis added instead. [Bug #17429]
-
-
3.0
-
Module#includeandModule#prependnow affect classes and modules that have already included or prepended the receiver, mirroring the behavior if the arguments were included in the receiver before the other modules and classes included or prepended the receiver. [Feature #9573]Exampleclass C; end module M1; end module M2; end C.include M1 M1.include M2 p C.ancestors #=> [C, M1, M2, Object, Kernel, BasicObject]
-
Module#public,Module#protected,Module#private,Module#public_class_method,Module#private_class_method, toplevel “private” and “public” methods now accept single array argument with a list of method names. [Feature #17314] -
Module#attr_accessor,Module#attr_reader,Module#attr_writerandModule#attrmethods now return an array of defined method names as symbols. [Feature #17314] -
Module#alias_methodnow returns the defined alias as a symbol. [Feature #17314]
-