Strings
Strings are the first data type we’ll know inside Ruby. A string is traditionally a sequence of characters. For instance the string: “Hello World!” contains a total of 12 characters (including space and “!” symbol). As we can see is a sequence of characters and in computer programming is considered a data structure because each character is internally separated. Let’s check it out visually:
A string is generally considered as a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding. String may also denote more general arrays or other sequence (or list) data types and structures. Later we’ll see what is an array, for now let’s just test it out in our IRB console
$ irb
2.6.8 :002 > "Hello World!"
The first rule of the strings in ruby is that they need to be inside quotes (double or single). Once we hit enter, we can see the output of our string
Another way we have to print a string is using the reserved word “puts” or “print”. The puts (short for “put string”) and print commands are both used to display the results of evaluating Ruby code. The primary difference between them is that puts adds a newline after executing, and print does not.
2.6.8 :002 > puts "Hello World!"
2.6.8 :003 > print "Hello World!"
Is Ruby we use frequently the word “puts”
Arrays
Array is a data type in Ruby. An array is a container of other ruby data type elements. The syntax starts with an open square bracket “[“ then inside of it we put the elements separated by comma and finally closing the array with a closed square bracket “]”. So:
* An array is an ordered set of objects in Ruby.
* An array begins and ends with square brackets ([ and ]).
* Each item is separated by a comma (e.g. 67, 68).
* It is considered good practice to add a space after the comma.
Examples:
2.6.8 :001 > numbers = [1, 2, 3, 4]
=> [1, 2, 3, 4]
An Array can contain more than just integer numbers.
2.6.8 :004 > names = ["Chris", "Daniel", "Dakota", "Emily"]
=> ["Chris", "Daniel", "Dakota", "Emily"]
We have seen that arrays can contain integers and strings. Arrays can also contain other arrays!
2.6.8 :005 > age = [["Chris", 30], ["Daniel", 28], ["Dakota", 25], ["Emily", 26]]
=> [["Chris", 30], ["Daniel", 28], ["Dakota", 25], ["Emily", 26]]
Strings and Arrays
You could be asking yourself why we are seeing Arrays inside this blog post? Well is because arrays and strings are almost the same data type, just with a couple of differences
- - Strings behave as an Array under the hood
- - A string is basically an array of characters in Ruby
- - That means that you can do almost the same operations with strings and arrays
Let’s see an example of this:
2.6.8 :072 > string = "Hello World!"
=> "Hello World!"
2.6.8 :073 > string[0]
=> "H"
2.6.8 :074 > string[1]
=> "e"
2.6.8 :075 > string[2]
=> "l"
2.6.8 :076 > string[3]
=> "l"
2.6.8 :077 > string[-1]
=> "!"
2.6.8 :078 > string[1,3]
=> "ell"
2.6.8 :079 > string[0] = "h"
=> "h"
2.6.8 :080 > string
=> "hello World!"
Let’s explain the code line by line
- - In the first line we’ve created an string and assigned to a variable
- - Then we started accessing each element of the string as an array and doing something like
- - string[0] and we get the “H” word, which is the first word on the string, and the position zero of the array.
- - string[1] and we get the “e” word, which is the second word on the string, and the position one of the array.
- - string[2] and we get the “l” word, which is the third word on the string, and the position two of the array.
- - string[3] and we get the “l” word, which is the fourth word on the string, and the position four of the array.
- - string[-1] and we get the “!” word, which is the last character on the string, and the position -1 of the array.
- - string[1, 3] and we get the “ell” word, which is the three characters on the string from the position 1 to 3.
- - String[0] = “h” here we’re assignin (or updating) the array (or string) at the position zero for the same word but lower cased. And when we print out the variable again we can see the result of this update.
That’s one of the magics of the programming languages, these fancy things between strings and arrays lead us to ask if we can use other methods related to arrays into de strings. Let’s see
2.6.8 :087 > array = [1, 2, 3, 4]
=> [1, 2, 3, 4]
2.6.8 :088 > array.length
=> 4
2.6.8 :089 > string = "Hello"
=> "Hello"
2.6.8 :090 > string.length
=> 5
As you can see we can apply the same method for arrays and strings. The method length to know how large is each array or string.
2.6.8 :092 > array = [1, 2, 3, 4]
=> [1, 2, 3, 4]
2.6.8 :093 > array += [5, 6]
=> [1, 2, 3, 4, 5, 6]
2.6.8 :094 > string = "Hello"
=> "Hello"
2.6.8 :095 > string += "world!"
=> "Helloworld!"
2.6.8 :096 > string + "world!"
=> "Helloworld!world!"
Also we can add new values to the array or to the string, following the “+” sign or the “+=” sign
we saw in the Array post.
Other well known example here is this one:
2.6.8 :112 > string = "Hello World!"
=> "Hello World!"
2.6.8 :113 > string.split(" ")
=> ["Hello", "World!"]
2.6.8 :114 > ["Hello", "World!"].join(" ")
=> "Hello World!"
Explanation
- - The .split() method separates a string with a given parameter, in this case the blank space and the result will be an array with all the elements who meet the condition. So in thi case we end with an array of two elements: ["Hello", "World!"]
- - On the other hand we have the .join() method which re-unite the elements from a given array by the separator we pass inside the parameter (in this case a blank space) and we end with the string "Hello World!"
Differences between Arrays and Strings
However we’ve to take care about the differences between these two types of data types in Ruby, because finally we are dealing with two different data types, regardless of their coincidences or similarities. For instance, if we are going to use the “Each” method to do something inside of them, we’ll see an error with the string. Let’s do it
2.6.8 :097 > array = [1, 2, 3, 4]
=> [1, 2, 3, 4]
2.6.8 :098 > array.each do |element|
2.6.8 :099 > puts element
2.6.8 :100?> end
1
2
3
4
=> [1, 2, 3, 4]
2.6.8 :101 > string = "Hello"
=> "Hello"
2.6.8 :102 > string.each do |element|
2.6.8 :103 > puts element
2.6.8 :104?> end
Traceback (most recent call last):
4: from /home/daniel/.rvm/rubies/ruby-2.6.8/bin/irb:23:in `<main>'
3: from /home/daniel/.rvm/rubies/ruby-2.6.8/bin/irb:23:in `load'
2: from /home/daniel/.rvm/rubies/ruby-2.6.8/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
1: from (irb):102
NoMethodError (undefined method `each' for "Hello":String)
Here we'll have a NoMethodError because the method “Each” was created just for Arrays. This worked with Ruby 1.8, but after Ruby 1.9 it doesn’t work anymore, so we’ve to use another fancy method if we definitely want to iterate through each character in the array, the method is called: “each_char”. Let’s see it
2.6.8 :108 > string = "Hello"
=> "Hello"
2.6.8 :109 > string.each_char do |element|
2.6.8 :110 > puts element
2.6.8 :111?> end
H
e
l
l
o
=> "Hello"
Pretty awesome! I think that at this point is clear what we have to take into mind: “Arrays and Strings are similar in some nature, but at the same time they are different when we need to do some kind of operations with them”
Other String methods
Now that we know the difference between arrays and strings, let’s use other kind of string methods in ruby
2.6.8 :115 > "Hi " * 5
=> "Hi Hi Hi Hi Hi "
2.6.8 :116 > "hi there".capitalize
=> "Hi there"
2.6.8 :117 > "hi there".upcase
=> "HI THERE"
2.6.8 :118 > "HI THERE".downcase
=> "hi there"
2.6.8 :119 > "hi there".empty?
=> false
Let’s explain
- - First line the string “Hi “ is repeated by 5 times because we’re multiplying the string
- - The method “.capitalize” receives the string and then capitalize it
- - The method “.upcase” receives the string and then upcase it
- - The method “.downcase” receives the string and then downcase it
- - The method “.empty” receives the string and then evaluates if it is empty and returns a Boolean type.
Note: all of these methods help us to do common tasks and they come with the Ruby programming language itself. The name for this is built-in methods. If you want an exhaustive list of string methods, please refer to this link:
https://ruby-doc.org/core-3.1.0/String.html and on the left panel you’ll find all the possible methods, also if you scroll the page
I hope you learnt a lot about strings in this post.
Thanks for reading!
DanielM