Kernel#abort

abort([msg])
Returns: nil · Updated March 13, 2026 · Kernel Methods
process exit termination error

Kernel#abort terminates the Ruby program immediately without executing any at_exit blocks. It outputs an optional message to stderr and sets the process exit status to 1 (indicating failure).

Syntax

abort
abort("Error message")

Parameters

ParameterTypeDefaultDescription
msgStringnilOptional error message written to stderr before termination

Examples

Basic usage

# Abort without a message
abort

# Outputs nothing to stdout, returns exit code 1

With an error message

def validate_age(age)
  abort "Age cannot be negative" if age < 0
  puts "Valid age: #{age}"
end

validate_age(-5)
# => (stderr: "Age cannot be negative")
#    exit code: 1

In a script context

#!/usr/bin/env ruby
# check_file.rb

filename = ARGV[0]
abort "Usage: check_file.rb <filename>" if filename.nil?

unless File.exist?(filename)
  abort "Error: File '#{filename}' not found"
end

puts "File exists: #{filename}"

Run with: ruby check_file.rb missing.txt Output to stderr: Error: File 'missing.txt' not found

Common Patterns

Using abort in validation

def process_data(input)
  return input if input.valid?
  
  abort "Invalid input received, stopping processing"
end

Checking for required dependencies

def ensure_gem(name)
  require name
rescue LoadError
  abort "Required gem '#{name}' is not installed. Run: gem install #{name}"
end

Differences from exit

  • abort does NOT run at_exit blocks
  • exit DOES run at_exit blocks
  • abort always returns exit code 1
  • exit can return a custom exit code

See Also

  • exit — terminates the program but runs at_exit blocks
  • raise — raises an exception instead of terminating