String#start_with?

str.start_with?(*prefixes) -> true or false
Returns: boolean · Updated March 13, 2026 · String Methods
strings prefix boolean

The start_with? method tests whether a string begins with a specified prefix. It accepts one or more prefixes and returns true if the string starts with any of them, otherwise false. This method is useful for validation, conditional logic, and parsing structured text. Unlike include?, which checks for substrings anywhere in the string, start_with? specifically looks at the beginning of the string.

The method performs an exact string match and is case-sensitive. If you need case-insensitive matching, you’ll need to convert both the string and prefix to the same case using downcase or upcase before calling start_with?.

Syntax

str.start_with?(*prefixes)

The splat operator (*) allows you to pass multiple prefixes. The method returns true if the string starts with ANY of the provided prefixes.

Parameters

ParameterTypeDefaultDescription
prefixesStringOne or more string prefixes to check against. At least one is required. Can be a single string or multiple strings using the splat operator.

Examples

Basic usage

"hello".start_with?("hel")
# => true

"hello".start_with?("world")
# => false

"hello".start_with?("hello")
# => true

Checking multiple prefixes

filename = "report_2024.pdf"

filename.start_with?("report", "summary", "analysis")
# => true

filename.start_with?("invoice", "receipt")
# => false

The multiple prefix feature is particularly useful for command routing and file type detection.

Case-sensitive matching

"Ruby".start_with?("ruby")
# => false

"Ruby".start_with?("Ruby")
# => true

# For case-insensitive matching:
"Ruby".downcase.start_with?("ruby")
# => true

Empty string behavior

"hello".start_with?("")
# => true

"".start_with?("")
# => true

"".start_with?("anything")
# => false

An empty prefix always returns true because every string technically starts with an empty string.

Common Patterns

Input validation

def process_command(input)
  if input.start_with?("exit") || input.start_with?("quit")
    puts "Goodbye!"
    exit
  elsif input.start_with?("help")
    show_help
  elsif input.start_with?("load ")
    load_file(input[5..])
  else
    execute(input)
  end
end

File type checking

ALLOWED_EXTENSIONS = %w[string-methods::]

def allowed_extension?(filename)
  ALLOWED_EXTENSIONS.any? { |ext| filename.start_with?(ext) }
end

# Usage
allowed_extension?("document.pdf")      # => true
allowed_extension?("image.png")          # => false

URL parsing

def secure_url?(url)
  url.start_with?("https://")
end

def http_method(method)
  valid_methods = %w[GET POST PUT DELETE PATCH]
  valid_methods.any? { |m| method.start_with?(m) }
end

Routing logic

def route_request(path)
  case path
  when ->(p) { p.start_with?("/api/") }
    handle_api(path)
  when ->(p) { p.start_with?("/admin") }
    handle_admin(path)
  when ->(p) { p.start_with?("/static") }
    serve_static(path)
  else
    handle_default(path)
  end
end

Using lambdas in case statements allows for more complex matching conditions.

Errors

The start_with? method does not raise any exceptions under normal circumstances. However, passing nil as a prefix will raise a NoMethodError:

"hello".start_with?(nil)
# => NoMethodError: undefined method `start_with?' for nil:NilClass

Always ensure your prefix values are strings before calling start_with?.

See Also