Kernel#warn

warn(string[, category: :optional]) -> true or false
Returns: Boolean · Updated March 13, 2026 · Kernel Methods
warnings stderr debugging deprecation

The warn method outputs a warning message to STDERR. It’s commonly used to alert users about deprecated features, potential issues, or informational notices during program execution.

Basic Usage

# Simple warning
warn "This feature will be removed in version 2.0"
# Outputs: Warning: This feature will be removed in version 2.0

# Multiple warnings
warn "Deprecation warning"
warn "Another warning"

Practical Examples

Deprecation Warnings

def old_method
  warn "old_method is deprecated, use new_method instead"
  new_method
end

def new_method
  # New implementation
end

Conditional Warnings

def process_data(data)
  if data.empty?
    warn "Warning: Empty data set provided"
    return
  end
  
  if data.size > 1000
    warn "Warning: Large dataset (#{data.size} items) may take longer"
  end
  
  # Processing...
end

With Category (Ruby 2.5+)

warn "Using deprecated API", category: :deprecated
warn "This is experimental", category: :experimental

# Suppress specific categories
# Run with: ruby -W:no-experimental script.rb

Verbose Mode Warnings

# Only warn in verbose mode
def experimental_feature
  warn "Experimental feature" if $VERBOSE
end

Comparing warn with other output methods

puts "Info message"       # Goes to STDOUT
warn "Warning message"   # Goes to STDERR
$stderr.puts "Error"    # Goes to STDERR (explicit)

Configuration

# Suppress warnings
$VERBOSE = nil   # Suppress all warnings

# Enable warnings
$VERBOSE = true   # Normal warnings

# Very verbose
$VERBOSE = 2      # Extra verbose (includes deprecated warnings)

The warn method is the idiomatic way to output warnings in Ruby, distinguishable from regular output by going to STDERR.

See Also