DEV Community

Cover image for Simply Strings in Ruby
Merdan Durdyyev
Merdan Durdyyev

Posted on

Simply Strings in Ruby

Welcome to my next article dear readers, coders, and enthusiasts. This time, let's take a look at the "String" data type in Ruby.
I will describe the main features of strings and show you some useful methods to use with them as well.

The first thing we have to note is that all the strings are instances of the "String" class. The other thing is that string is just a series of characters. Even a single character can represent a string. A string with no characters at all is also a string, but an empty string.

Creating a new string

To create a new string we use the "new" method or we can also assign a string value to a new variable.

# - using a method.
book_title ="Charlie and the chocolate factory")
# Creating a new string with a second method. 
# Just assigning it to a new variable.
another_book = "Harry Potter and the Sorcerer's Stone"
last_book = 'The first teacher'
Enter fullscreen mode Exit fullscreen mode

You can enclose strings within single quotes (‘ ‘) and double quotes as well (“ “). Both of them represent string value, but there's a slight difference in usage.
If you are to use a single quote in the string, then you must enclose it to double-quotes. For instance :

# There is a single quote after character 'e'.
# If we write it as : 'The Architect's Apprentice'
# the interpreter will not understand where it starts
# and where it ends.
# You will see an error in the program.
book_title = "The Architect's Apprentice"
Enter fullscreen mode Exit fullscreen mode

String Interpolation

The advantage of enclosing strings within double quotes is that you can use something named as interpolation. For instance, if you want to print a string and a number at the same time, you will have to convert the number to a string using the "to_s" method of the number.

age = 25
# You will have an error on the line below.
puts "I am " + age + " years old."   # ERROR
# The right way (to use "to_s" method)
puts "I am " + age.to_s + " years old."
Enter fullscreen mode Exit fullscreen mode

Using "interpolation", you can make it easier and you do not have to worry about converting the number to a string. The string interpolation process converts it for you automatically.

age = 25
# String interpolation
# We use (#) sign and special brackets {} to achieve this.
# So, the 'age' number variable is automatically converted
# to string type.
puts "I am #{age} years old."

puts "One plus one is #{1+1} !"
# Addition result is calculated in the brackets and 
# the result is converted to string.
# "One plus one is 2 !"
Enter fullscreen mode Exit fullscreen mode

Uppercase & Lowercase

String data is mostly comprised of letters and sometimes you need to convert them fully to uppercase letters or all of them to lowercase letters. In such cases, you can use built-in “downcase” and “upcase” methods of the String class.

first_name = "John"
puts full_name.upcase # "JOHN"

last_name= "Wick"
puts last_name.downcase # "WICK"
Enter fullscreen mode Exit fullscreen mode

Some additional methods you can use are “capitalize”, which converts the first symbol to uppercase (if it is a letter), “swapcase”, that reverses uppercase and lowercase letters (lowercase letters are converted to uppercase and uppercase letter, to lowercase).

word = "Hello"
puts word.capitalize # "HELLO"
sentence = 'Who are you?"
puts sentence # "wHO ARE YOU?"  
Enter fullscreen mode Exit fullscreen mode


To join several string values we usually use the (+) sign. The process of joining multiple strings is called "concatenation".

# We join multiple strings and output to the screen.
first_name = 'John'
last_name = "Wick"
age = 35
puts "My full name is " + first_name + " " + last_name
puts "I am " + age.to_s + " years old"
Enter fullscreen mode Exit fullscreen mode

Length of the string

In most programming languages, to get the number of characters in the string we use either the "size" or "length" method. We can easily confuse which one to use, or which one is built-in in Ruby. To make that easier for coders, developers of Ruby included both of the methods. So you do not have to try to remember which one to use in Ruby. You just write either one you remember.

example = 'Come here'
puts example.size # 9
one_more = "Where?"
puts one_more.length # 6
Enter fullscreen mode Exit fullscreen mode

Besides that, there is also another useful method you can use to find out if the string has at least one character. For this situation, we use "empty?" methods and it returns either "true" or "false" depending on whether the length of the string is 0 or not.

"Hello".empty? #=> false
"!".empty?     #=> false
" ".empty?     #=> false
"".empty?      #=> true
Enter fullscreen mode Exit fullscreen mode

Searching a substring within a string

It is a usual situation when we need to find out whether a smaller string is a substring of a larger one, and if yes, then where it starts (at what index). We have several built-in methods to use as helpers.

  • start_with? (check whether a string starts with another)
  • end_with? (check whether a string ends with another)
  • include? (check whether the smaller string is included within the larger one)
  • index (find out the index, from which the substring starts within a larger string)
# "start_with?" funksiýasy 
string = "ruby programming"
string.start_with? "ruby" 
# true
string.start_with? "java" 
# false

# "end_with?" method
string = "ruby programming"
string.end_with? "programming"
# true
string.end_with? "coding"
# false

string = "Today is Sunday"
# 2
# indexing starts from 0
# 0-'T', 1-'o', 2-'d', 'day' starts at index 2.
Enter fullscreen mode Exit fullscreen mode

Slicing strings

To cut a slice out of a string, we image the string as a series of characters or an array. After that, all we have to do is to show what range of characters we have to slice. We show it within square brackets "[ ]". For instance:

string = "Arividerci"

# "Ari"
# start from index 0 and count 3 characters

# "vider"
# Start from index 3 and count 5 characters
Enter fullscreen mode Exit fullscreen mode


Dear friends and readers. Finally, we came to an end of our article about using Strings in our beloved Ruby PL. Auxiliary String methods we looked through are just a limited set of them. I listed just the most useful ones as I thought. I thought they will be easier to use and keep in memory. To learn more methods you will have to go to the documentation page.
This is also enough to start using Ruby and to start loving it as well :-) Good luck to you, and hope to see you soon!

Top comments (0)