Ruby undef Keyword
last modified October 18, 2026
This tutorial explains how to use Ruby's undef keyword. It removes
method definitions from the current class or module.
The undef keyword marks a method as undefined in the class or module
where it appears. Instances of that class, or classes that include the module,
then raise NoMethodError when they call the method. Inside a class
body, undef targets instance methods; class methods must be
undefined in the class's singleton class.
undef is useful when you need to restrict certain functionality.
It works with both built-in and user-defined methods. The change is not
permanent: reopening the class and defining the method again restores it.
Basic undef Example
This simple example demonstrates removing a method from a class. After using
undef, calling the method raises an error.
class Greeter
def hello
puts "Hello!"
end
end
greeter = Greeter.new
greeter.hello # Works fine
class Greeter
undef hello
end
greeter.hello # Raises NoMethodError
The hello method is first defined and works normally. After
undef hello, calling it raises NoMethodError.
undef with Inherited Methods
undef can block methods inherited from parent classes. This example
shows how to prevent method inheritance.
class Parent
def inherited_method
puts "From parent class"
end
end
class Child < Parent
undef inherited_method
end
Child.new.inherited_method # Raises NoMethodError
Parent.new.inherited_method # Still works
The child class blocks the inherited method while the parent keeps it. The
undefined entry stops method lookup in Child, so the call raises
NoMethodError.
undef Multiple Methods
Ruby allows removing multiple methods with a single undef
statement. Separate method names with commas.
class MultiMethod def method1; end def method2; end def method3; end end class MultiMethod undef method1, method2 end obj = MultiMethod.new obj.method3 # Works obj.method1 # Raises NoMethodError obj.method2 # Raises NoMethodError
Three methods are defined initially. The undef statement removes
two of them at once. The remaining method stays available.
undef with Built-in Methods
You can use undef to make inherited core methods unavailable in a
class. This example disables the to_s method.
class CustomObject undef to_s end obj = CustomObject.new puts obj # Raises NoMethodError
The to_s method is removed from CustomObject. Any
attempt to convert the object to a string, including puts, now
fails. This is one reason to use undef with care.
undef in Modules
undef works in modules too. This example shows method removal
before including the module.
module MyModule
def module_method
puts "From module"
end
undef module_method
end
class MyClass
include MyModule
end
MyClass.new.module_method # Raises NoMethodError
The method is defined then immediately removed in the module. Classes including the module won't have access to it. This pattern can control exposed methods.
undef vs remove_method
Ruby also has remove_method which differs from undef.
This example shows the key difference.
class Parent
def example
puts "Parent implementation"
end
end
class Child < Parent
def example
puts "Child implementation"
end
remove_method :example # Removes Child's version, Parent's remains
# undef example # Blocks lookup and raises NoMethodError
end
Child.new.example # Outputs "Parent implementation"
remove_method deletes only the method defined directly in
Child, so Ruby continues up the ancestor chain and finds
Parent#example. undef instead installs an undefined
entry that stops lookup, so Child.new.example would raise
NoMethodError even though the parent still defines the method.
undef_method behaves like undef but can be called
programmatically. Use remove_method to fall back to an ancestor's
implementation, and undef or undef_method to block the
method entirely.
Dynamic undef with send
You can remove methods dynamically with undef_method. Because it is
a private method of Module, send lets you call it with
a computed method name at runtime.
class DynamicClass def method1; end def method2; end end [:method1, :method2].each do |name| DynamicClass.send(:undef_method, name) end obj = DynamicClass.new obj.respond_to?(:method1) # => false obj.respond_to?(:method2) # => false
The example removes methods in a loop. undef_method is the
programmatic counterpart of undef, and the methods become
unavailable on instances of the class.
Source
This tutorial covered Ruby's undef keyword with practical examples showing method removal in various contexts. Use it carefully, since it makes the method unavailable until it is defined again.
Author
List all Ruby tutorials.