Last week we began our adventure into the wonderful world of Ruby by looking at some of the reasons why you would want to invest your time into learning it.
We also set up our computers to run Ruby and we started to play around with what we can do with numbers.
Strings are one of the foundational data types of any programming language.
In today’s tutorial we continue to use IRB to understand how we can use and manipulate strings in Ruby.
If you missed last week’s tutorial, you might want to read that before continuing with today’s tutorial.
What is a string?
Before we get into playing with strings in IRB, first it’s important to understand what a string actually is.
A string in computer programming is one of the primitive data types of a programming language. Strings are typically used as literal constant or as some kind of variable.
For example a string might be used to store the user’s name:
name = "Jane"
In Ruby, a string is an object that holds a sequence of characters. You can create a new string by creating a new object, or simple creating a string literal.
String object has a number of useful methods that can be invoked in order to manipulate the string.
In today’s tutorial we will be exploring the methods of the
Fire up IRB by going into your terminal and typing:
irb [/bash] Next create a new string: ```ruby name = "Jane"
name is now an instance of the
String ruby object. We can check to make sure this is the case by calling the
name.class => String
In the example above we create a new
String object as a literal. However we can also instantiate a
String using the
person = String::new(‘Bob’) => "Bob" person.class => String
Now that we know how to create new instances of the
String object, we can start to explore some of the instance methods of the class.
One of the most basic things you will find yourself doing with strings is concatenation. This is where you join two strings together. There are a few different ways to do this in Ruby.
Firstly you can join two strings together using the
greeting = "hello " + "world" => "hello world"
If you already have a string you can append another string using the ```
name = "Philip "<br></br> => "Philip "``` name << "Brown" => "Philip Brown"
You can also multiple a string by an integer to return three copies of that string as a new string:
"yo" * 3 => "yoyoyo"
We can convert a string into capital case by calling the
"philip".capitalize => "Philip"
When you call a method on a string, such as
capitalize, you will be returned a new string.
For example, try the following in IRB:
name = "philip" => "philip" name.capitalize => "Philip"
Now if you
name variable, what do you think it will be?
puts name => "philip"
When you call a method on a
String object it creates a new string, rather than changing the existing one.
To modify the original instance we can call the same method but with a
name.capitalize! => "Philip" name => "Philip"
If you want to convert a string to lowercase you can call the
"HELLO WORLD".downcase => "hello world"
Alternatively you can covert a string to uppercase using the
"hello world".upcase => "HELLO WORLD"
If you need to count the number of characters in a string you can call use the
"abcdefghi".length => 9
Whenever you accept input from a user you should make sure they haven’t included any whitespace. You can remove white space using the
"philip brown ".strip => "philip brown"
If you want to modify the current instance of the string rather than create a new string, you should call the method with a
name = " philip brown " => " philip brown " name.strip! => "philip brown" name => "philip brown"
Alternatively you can only remove whitespace from the left or the right of the string by calling
There are many more useful
String methods available that make working with strings in Ruby a breeze, however there isn’t much point in memorising all of these methods when the Ruby documentation is only a Google search away.
Firstly, the two important things to take away from this tutorial is, when you call a method on a
String instance you will be returned a new object.
Secondly, if you want to modify the original object, you should call the method with a
I think rather than learning the syntax to every method of every object, a much better way of learning a new language is to understand the rules around how the language works.
When you can think in terms of the language you will have a much deeper understanding and you will know the right questions to ask when you get stuck on a particular problem.