6. Strings

As we talked about before, a string is basically just a piece of text. Ruby knows what is a string when you wrap text in between " or ' (double quotes or single quotes).

The difference in double quotes and single quotes is that double quotes allow you to embed Ruby code with #{} and single quotes don't.

Let's create a new file called strings.rb.

Inside this file, let's type in the following:

puts "10 times 3.14 is #{10 * 3.14}"
puts "10 times 3.14 is #{10 * 3.14}"

Save the file and run the program:

ruby string.rb

The first line of code is using double quotes, and the second line of code is using single quotes. Notice how the second line didn't evaluate the math equation.

Methods

In Ruby, every data type such as strings, integers, and dates are objects. For now, let's think of objects as things that receive commands to execute. Once we go into object oriented programming (OOP), we will talk more about objects.

Methods on the other hand, are the commands we tell the objects to execute.

For example "This is a string" is a String object. We can command it to reverse itself with the .reverse method:

"This is a string".reverse
=> "gnirts a si sihT"

Again, in this case "This is a string" is a String object, and .reverse is the method. "This is a string" is receiving the command to be reversed.

An easy way to remember: Objects are nouns, methods are verbs. (String is a noun, reverse is a verb)

String Methods

There are many string methods in Ruby, which you can view here if you are really interested. For now, let's just go through some of the more commonly used ones.

Create a new file called string_methods.rb. Inside this file write the following lines of code (don't copy and paste! Muscle memory is important):

# Makes all letters uppercase
puts "this is a string".upcase

# Makes all letters lowercase
puts "THIS IS A STRING".downcase

# Get length of string
puts "this is a string".length

# Reverse string
puts "this is a string".reverse

# Capitalize first letter
puts "this is a string".capitalize

# Check if string exists within string
puts "this is a string".include?("ri")

puts "this is a string".include?("jy")

# Split string into an array of individual characters
puts "this is a string".split("")

# Convert string into integer
puts "10".to_i

# Convert string into float
puts "10".to_f

Save the file and run the program.

ruby string_methods.rb

As you can see, Ruby provides us with really convenient methods to do different things with strings.

Parameters and Arguments

Let's take a look at the following line of code that you just wrote:

"this is a string".include?("jy")

Notice the ("jy") after include?. Everything in between the parenthesis is called a parameter, or an argument. Both words mean the same thing.

Arguments are data that we pass into the method. The method takes the data and uses it to execute the method.

For example, the include? method takes in an argument and uses it to determine if the element passed in as the argument exists inside the string.

We also pass in an argument for the split method:

"this is a string".split("")

In this case, the split method uses the parameter to split the string. If we pass in another parameter, the method will split the string in a different way:

"this is a string".split("i")
=> ["th", "s ", "s a str", "ng"]

As you can see, the split method is splitting the string by the letter i.

In case you were wondering, for the most part, you don't need to memorize these methods. Instead of memorizing, even professional programmers Google for the correct method to use all of the time.

Assignment

Write a Ruby program that does the following: