Home » Ruby programming

Ruby Strings

Ruby Strings: In this tutorial, we are going to learn about the strings in the Ruby programming language, like string formation, puts statement, string containers, string concatenation, string interpolation, etc.
Submitted by Hrithik Chandra Prasad, on July 28, 2019

Ruby Strings

We know that Strings are the sequence of characters which are used to represent some texts; they may also contain some numbers, spaces, and symbols. For example, "Mangoes" and "There are 3 mangoes!" both are strings. Generally, for identification purpose strings are enclosed in quotation marks.

Let us understand, what strings are meant in Ruby?

Strings are considered as objects in Ruby. You will frequently use strings while programming. You will use them to communicate with users by displaying messages in the form of messages on the Interface.

String formation

In Ruby, strings can be formed using single quotes ' ' or double quotes " ". As far as you are consistent, both the cases are kind of same. You will find the difference during string interpolation. The string can be created in the following way:

    'I love Includehelp'
    #or
    "I love the tutorials of Includehelp"

Communicating through Strings:

You can print a string with the help of print and puts statement in the following way:

# Using the print statement:
print "Hello there!"
print "Welcome to Includehelp."
print "Go through the tutorials of your choice!"

# Using puts statement:
puts # to print a new line only
puts "Hello there!"
puts "Welcome to Includehelp."
puts "Go through the tutorials of your choice!"

Output

Hello there!Welcome to Includehelp.Go through the tutorials of your choice!
Hello there! 
Welcome to Includehelp.
Go through the tutorials of your choice!

You can note that with puts statement, the strings are getting printed in different lines but this is not the case of print statement.

String containers

We often need strings to be stored in some space or memory, we use variables for that purpose. Strings can be stored in the variables using assignment operator = and single quotes ' ' or double quotes " " in the following manner:

# assigning string to a variable
variable_string = 'Hi there!'

# printing string 
puts variable_string

Output

Hi there!

It enhances the code reusability as we don't have to type same string multiple times, just call the variable whenever you need the string.

String concatenation

Concatenation simply means joining the two strings together. Two or more than two strings can be concatenated by using + operator in the following way:

# concatenating two strings
result_1 = "Include"+"help"

# assigning two strings in variables
string_1 = "Hello"
string_2 = "World!"

# concatenating string variables with space in between 
result_2 = string_1 + " " + string_2

# printing values/strings (i.e. results)
puts result_1
puts result_2

Output

Includehelp
Hello World!

Now let us check whether we can concatenate an integer variable with string variable or not with the help of following code:

Example:

# string 
string1="Roll no: "
# integer 
roll=27

# concatenating and assigning result to f_str
# it will return an error
f_str=string1+roll

# printing the result 
puts f_str

Output

main.rb:7:in '+': can't convert Fixnum into String (TypeError)
        from main.rb:7:in '<main>'

The compiler will reflect an error as there will be no implicit, conversion of fixNum to string. The objective can be achieved by applying .to_s() method like

# string 
string1="Roll no: "
# integer 
roll=27

# concatenating and assigning result to f_str
# converting fixNum to string using .to_s() method
f_str=string1+roll.to_s

# printing the result 
puts f_str

Output

Roll no: 27

The .to_s method will convert any data type variable into string type.

String Interpolation

Concatenation makes the output hard to debug and complex. String Interpolation provides an easy way to the purpose by embedding variables into the strings with the help of # and curly braces {} in the following way:

# assigning strings to the variables
str1 = "Hi there!"
str2 = "Includehelp.com"

# printing the value of the variables 
puts "#{str1} This is #{str2}"

Output

Hi there! This is Includehelp.com

Implicit typecasting of variables into strings can also be achieved using String Interpolation, consider the given example

# assigning string and an integre values 
# to the variables
str1 = "Hi there!"
marks = 89

# Implicit typecasting using String Interpolation
puts "#{str1} You got #{marks} marks"

Output

Hi there! You got 89 marks

You will notice that here we don't have to put any .to_s method for conversion purpose.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.