Home » Ruby programming

concat() method in Ruby

Ruby concat() method: Here, we are going to learn about the concat() method in Ruby programming language which is used to concatenate the strings.
Submitted by Hrithik Chandra Prasad, on September 14, 2019

Ruby concat() method

Ruby provides you various ways through which you can join two or more strings and store it into another memory for future utilization. The concat() method is also one of them. It provides you a way through which you can join two or more string objects and store the result into the same memory. You can even store the resultant string object as well into another string object by assigning a value to it.

The concat() method is considered as one of the fastest ways to carry out concatenation as it makes use of the same object to store the resultant string object. It makes the execution faster as compared to other ways of string concatenation.

It takes a string object or normal string as a parameter. If an integer value is provided to it, it implicitly converts it into the corresponding character depending upon its ASCII code.

The concat() method is implemented in the following way:

    String_Object.concat (String_Object)

Now let us go through its implementation in Ruby codes for a better understanding of the method.

Example 1:

Ruby program to concat strings 
using concat() method

puts "Enter Any String"
str = gets.chomp

for i in 1..10

puts "String with random numbers : #{str}"


RUN 1:
Enter Any String
String with random numbers : Cat1101544150

RUN 2:
Enter Any String
String with random numbers : Dog1233522008


In the above code, we are making use of the concat() method to add random numbers (which are generated by rand() method) with the string str. We have to convert the random number into string otherwise it will return the character associated with the ASCII value.

Example 2:

Ruby program to concat strings 
using concat method

puts "Enter 10 names of the students"
str = ""

for i in 1..10
	puts " #{i} Enter name:"
	nam = gets.chomp
	str.concat(nam.concat(" "))

puts "The names are #{str}"


Enter 10 names of the students
 1 Enter name:
 2 Enter name:
 3 Enter name:
 4 Enter name:
 5 Enter name:
 6 Enter name:
 7 Enter name:
 8 Enter name:
 9 Enter name:
 10 Enter name:
The names are Harish Kranthi j.Ai Vibha Namita Rajiv Vibha Nityasha Riya Somya


In the above code, we are calling the concat() method twice. One concat() method is being called under another concat() method. We are taking multiple names from the user inside the loop. In the last, we are printing the string object which has all the values entered by the user.

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.