Prevents the current class from responding to calls to the named method. Contrast this with remove_method
, which deletes the method from the particular class; Ruby will still search superclasses and mixed-in modules for a possible receiver. String
arguments are converted to symbols.
class Parent def hello puts "In parent" end end class Child < Parent def hello puts "In child" end end c = Child.new c.hello class Child remove_method :hello # remove from child, still in parent end c.hello class Child undef_method :hello # prevent any calls to 'hello' end c.hello
produces:
In child In parent prog.rb:23: undefined method 'hello' for #<Child:0x401b3bb4> (NoMethodError)
def module_keyword
: () -> String
Invokes Module.prepend_features
on each parameter in reverse order.
Import class refinements from module into the current class or module definition.
Returns an array of modules defined within the receiver.
module A refine Integer do end refine String do end end p A.refinements
produces:
[#<refinement:Integer@A>, #<refinement:String@A>]
Registers _filename_ to be loaded (using Kernel::require) the first time that _const_ (which may be a String or a symbol) is accessed in the namespace of _mod_. module A end A.autoload(:B, "b") A::B.doit # autoloads "b"
If const in mod is defined as autoload, the file name to be loaded is replaced with filename. If const is defined but not as autoload, does nothing.
Returns filename to be loaded if name is registered as autoload
in the namespace of mod or one of its ancestors.
module A end A.autoload(:B, "b") A.autoload?(:B) #=> "b"
If inherit
is false, the lookup only checks the autoloads in the receiver:
class A autoload :CONST, "const.rb" end class B < A end B.autoload?(:CONST) #=> "const.rb", found in A (ancestor) B.autoload?(:CONST, false) #=> nil, not found in B itself
The equivalent of included
, but for extended modules.
module A def self.extended(mod) puts "#{self} extended in #{mod}" end end module Enumerable extend A end # => prints "A extended in Enumerable"
The equivalent of included
, but for prepended modules.
module A def self.prepended(mod) puts "#{self} prepended to #{mod}" end end module Enumerable prepend A end # => prints "A prepended to Enumerable"
Case Equality—Returns true
if obj is an instance of mod or an instance of one of mod’s descendants. Of limited use for modules, but can be used in case
statements to classify objects by class.
Equality — At the Object
level, ==
returns true
only if obj
and other
are the same object. Typically, this method is overridden in descendant classes to provide class-specific meaning.
Unlike ==
, the equal?
method should never be overridden by subclasses as it is used to determine object identity (that is, a.equal?(b)
if and only if a
is the same object as b
):
obj = "a" other = obj.dup obj == other #=> true obj.equal? other #=> false obj.equal? obj #=> true
The eql?
method returns true
if obj
and other
refer to the same hash key. This is used by Hash
to test members for equality. For any pair of objects where eql?
returns true
, the hash
value of both objects must be equal. So any subclass that overrides eql?
should also override hash
appropriately.
For objects of class Object
, eql?
is synonymous with ==
. Subclasses normally continue this tradition by aliasing eql?
to their overridden ==
method, but there are exceptions. Numeric
types, for example, perform type conversion across ==
, but not across eql?
, so:
1 == 1.0 #=> true 1.eql? 1.0 #=> false
Comparison—Returns -1, 0, +1 or nil depending on whether module
includes other_module
, they are the same, or if module
is included by other_module
.
Returns nil
if module
has no relationship with other_module
, if other_module
is not a module, or if the two values are incomparable.
Returns true if mod is a subclass of other. Returns false
if mod is the same as other or mod is an ancestor of other. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A < B” implies “A < B”.)
Returns true if mod is a subclass of other or is the same as other. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A < B” implies “A < B”.)
Returns true if mod is an ancestor of other. Returns false
if mod is the same as other or mod is a descendant of other. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A < B” implies “B > A”.)
Returns true if mod is an ancestor of other, or the two modules are the same. Returns nil
if there’s no relationship between the two. (Think of the relationship in terms of the class definition: “class A < B” implies “B > A”.)
Returns the name of the module mod. Returns nil
for anonymous modules.
The first form is equivalent to attr_reader
. The second form is equivalent to attr_accessor(name)
but deprecated. The last form is equivalent to attr_reader(name)
but deprecated. Returns an array of defined method names as symbols.
Creates a new anonymous module. If a block is given, it is passed the module object, and the block is evaluated in the context of this module like module_eval
.
fred = Module.new do def meth1 "hello" end def meth2 "bye" end end a = "my string" a.extend(fred) #=> "my string" a.meth1 #=> "hello" a.meth2 #=> "bye"
Assign the module to a constant (name starting uppercase) if you want to treat it like a regular module.
With no arguments, sets the default visibility for subsequently defined methods to public. With arguments, sets the named methods to have public visibility. String
arguments are converted to symbols. An Array
of Symbols and/or Strings is also accepted. If a single argument is passed, it is returned. If no argument is passed, nil is returned. If multiple arguments are passed, the arguments are returned as an array.
With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility. String
arguments are converted to symbols. An Array
of Symbols and/or Strings is also accepted. If a single argument is passed, it is returned. If no argument is passed, nil is returned. If multiple arguments are passed, the arguments are returned as an array.
module Mod def a() end def b() end private def c() end private :a end Mod.private_instance_methods #=> [:a, :c]
Note that to show a private method on RDoc
, use :doc:
.
Returns an array of all modules used in the current scope. The ordering of modules in the resulting array is not defined.
module A refine Object do end end module B refine Object do end end using A using B p Module.used_modules
produces:
[B, A]
Invoked as a callback whenever an instance method is removed from the receiver.
module Chatty def self.method_removed(method_name) puts "Removing #{method_name.inspect}" end def self.some_class_method() end def some_instance_method() end class << self remove_method :some_class_method end remove_method :some_instance_method end
produces:
Removing :some_instance_method