On this page
End of Life Ruby 3.0 has reached end of life and gets no more fixes. 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 4
Instance methods 74
Ruby version changes 4
-
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]
-