String#end_with?
str.end_with?(*suffixes) -> true or false The end_with? method tests whether a string ends with a specified suffix. It takes one or more suffix arguments and returns true if the string terminates with any of them, otherwise false. This method is useful for validating file extensions, checking URL domains, and routing logic.
The match is exact and case-sensitive. There is no built-in option for case-insensitive matching — you must convert the string and suffix to the same case yourself if needed.
Syntax
str.end_with?(*suffixes)
The splat operator allows multiple suffixes. The method returns true if the string ends with any of the provided suffixes.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
*suffixes | String | — | One or more suffix strings to check. Pass each as a separate argument. |
Examples
Basic usage
"ruby".end_with?("y")
# => true
"ruby".end_with?("r")
# => false
"ruby".end_with?("by")
# => true
Multiple suffixes
"report.pdf".end_with?("pdf", "doc", "txt")
# => true
"image.png".end_with?("jpg", "gif", "svg")
# => false
"data.csv".end_with?("csv", "xlsx")
# => true
Case sensitivity
"Ruby".end_with?("ruby")
# => false
"Ruby".end_with?("Ruby")
# => true
# Case-insensitive version:
"Ruby".downcase.end_with?("ruby")
# => true
Empty suffix
"hello".end_with?("")
# => true
"".end_with?("")
# => true
An empty suffix always matches because every string ends with an empty string.
No arguments
"hello".end_with?()
# => true
Called with no arguments, end_with? returns true (vacuous truth — every string ends with nothing).
Common Patterns
File extension checking
def pdf_file?(filename)
filename.end_with?(".pdf")
end
def image_file?(filename)
filename.end_with?(".png", ".jpg", ".gif", ".webp")
end
pdf_file?("document.pdf") # => true
pdf_file?("document.PDF") # => false (case-sensitive)
image_file?("photo.jpg") # => true
URL and domain validation
def secure_url?(url)
url.end_with?("https://")
end
def asset_path?(path)
path.end_with?(".js", ".css", ".svg", ".png")
end
def markdown_file?(filename)
filename.end_with?(".md", ".markdown")
end
Conditional routing
case filename
when ->(f) { f.end_with?(".rb") }
run_ruby_file(filename)
when ->(f) { f.end_with?(".py") }
run_python_file(filename)
when ->(f) { f.end_with?(".js") }
run_javascript_file(filename)
else
unknown_file_handler(filename)
end
Checking Against an Array of Suffixes
If you have an array of suffixes, you must splat it when passing to end_with?:
extensions = [".pdf", ".doc", ".txt"]
"report.pdf".end_with?(extensions)
# => false (passes the array as a single argument, which won't match)
"report.pdf".end_with?(*extensions)
# => true (splats the array into separate arguments)
Without the splat, Ruby compares the string against the array object itself, not its elements.
Gotchas
Regexp is not supported
Unlike start_with?, end_with? does not accept a Regexp argument. Passing a Regexp will raise an ArgumentError or match against the object’s string representation:
"ruby123".end_with?(/\d+/)
# => false (attempts to match against the Regexp object's to_s)
# Use gsub or match instead for regex suffix matching:
"ruby123".match?(/\d+\z/)
# => true
Order of suffixes does not matter
"ruby".end_with?("y", "by") behaves the same as "ruby".end_with?("by", "y") — either match returns true.
Case sensitivity is strict
There is no end_with?(:ignore_case) option. Always normalize case if you need case-insensitive matching.
Errors
Passing nil as a suffix raises a NoMethodError:
"hello".end_with?(nil)
# => NoMethodError: undefined method `end_with?' for nil:NilClass
Always ensure suffix values are strings before calling end_with?.
See Also
- String#start_with? — checks if a string begins with a prefix
- String#include? — checks if a string contains a substring
- Kernel#chomp — removes trailing line separators
- String#strip — removes leading and trailing whitespace