Home »
Ruby »
Ruby Programs
Ruby program to demonstrate the string concatenation
Ruby Example: Write a program to demonstrate the string concatenation.
Submitted by Nidhi, on December 31, 2021
Problem Solution:
In this program, we will demonstrate the different ways of string concatenation and print the string after concatenation.
Program/Source Code:
The source code to demonstrate the string concatenation is given below. The given program is compiled and executed successfully.
# Ruby program to demonstrate the
# string concatenation.
# Concatenate using "+" operator.
MyStr1 = "Hello " + "World";
puts MyStr1;
# Concatenate using a single space.
MyStr2 = "Hello " "World";
puts MyStr2;
# Concatenate using "<<" operator.
MyStr3 = "Hello " << "World";
puts MyStr3;
# Concatenate using concat() method.
MyStr4 = "Hello ".concat("World");
puts MyStr4;
Output:
Hello World
Hello World
Hello World
Hello World
Explanation:
In the above program, we created string variables and assigned values by concatenating strings using different ways and printed the result.
Ruby Strings Programs »