String#concat

str.concat(other)
Returns: String · Updated March 13, 2026 · String Methods
ruby string concatenation joining

The concat method joins one or more strings to the receiver, modifying it in place. It accepts a single string, multiple string arguments, or even integer arguments which are converted to their character equivalents.

This method is equivalent to the + operator but can handle multiple arguments in a single call, making it slightly more convenient for joining several strings at once.

Basic Usage

The simplest form of concat joins a single string to the receiver:

str = "Hello"
str.concat(" World")
str  # => "Hello World"

"Ruby".concat(" is awesome")
# => "Ruby is awesome"

Multiple Arguments

Unlike the + operator, concat can accept multiple strings in a single call:

str = "Start"
str.concat(" ", "Middle", " ", "End")
str  # => "Start Middle End"

This can be cleaner than chaining multiple + operations:

# Using +
"a" + "b" + "c" + "d"  # => "abcd"

# Using concat with multiple args
"a".concat("b", "c", "d")  # => "abcd"

Integer Arguments

One unique feature of concat is that it accepts integer arguments, converting each to its corresponding character using the character encoding:

str = "Hello"
str.concat(33)  # ASCII 33 = "!"
str  # => "Hello!"

# Multiple integers
"".concat(72, 101, 108, 108, 111)  # "Hello"

This works with the integer Unicode code point, so you can also use it for emoji and other Unicode characters:

"Welcome".concat(128075)  # ๐Ÿ‘‹
# => "Welcome๐Ÿ‘‹"

The + Operator Alternative

The + operator is functionally equivalent to concat for single-string concatenation, but creates a new string rather than modifying in place:

# concat - modifies in place
a = "Hello"
a.concat(" World")
a  # => "Hello World"

# + - creates new string
b = "Hello"
b + " World"  # => "Hello World"
b            # => "Hello" (unchanged)

Performance Consideration

Since concat modifies the string in place, it avoids creating intermediate string objects. For building strings iteratively, using concat is more memory-efficient than using +:

# Less efficient - creates many intermediate strings
result = ""
%w[foo bar baz].each { |s| result += s }

# More efficient - modifies in place
result = ""
%w[foo bar baz].each { |s| result.concat(s) }

Practical Example: Building SQL Queries

concat is handy for building queries or URLs where you need to append multiple segments:

def build_query(base_url, params)
  url = base_url.dup
  params.each do |key, value|
    url.concat(key, "=", value, "&")
  end
  url.chomp("&")  # Remove trailing &
end

build_query("https://api.example.com?", { page: "1", limit: "10" })
# => "https://api.example.com?page=1&limit=10"

See Also

  • split - Splits a string into an array based on a delimiter
  • replace - Replaces the entire content of a string
  • array-join - Joins array elements into a string