Lesson 2
Revisiting Ruby Essentials: Arrays and Strings
Introduction

Welcome to the next unit of this course!

Before we delve deeper into essential Ruby concepts, particularly for interview preparation, we need to revisit some of Ruby's features—specifically, Ruby collections: Arrays and Strings. These collections allow Ruby to group multiple elements, such as numbers or characters, into a single entity.

Some of these concepts might already be familiar to you, so you can breeze through the beginning until we get to the more complex topics and paths.

Understanding Ruby's Collections

At our starting point, it's crucial to understand what Ruby collections are. They help us manage multiple values efficiently. We will mainly focus on Arrays and Strings in Ruby. An interesting fact here is that both arrays and strings are mutable in Ruby, meaning you can directly modify their contents. Let’s see examples:

Ruby
1# Defining an array and a string 2my_array = [1, 2, 3, 4] 3my_string = 'hello' 4 5# Now let's try to change the first element of both collections 6my_array[0] = 100 # Directly modifies the array to [100, 2, 3, 4] 7my_string[0] = 'H' # Directly modifies the string to "Hello" 8 9puts my_array.inspect # Output: [100, 2, 3, 4] 10puts my_string # Output: "Hello"

In this example, we see that both the array and the string were modified in place. Ruby’s mutability allows us to change them directly, which can be quite powerful!

Diving Into Arrays

Imagine having to take an inventory of all flora in a forest without an array at your disposal — seems nearly impossible, right? That's precisely the purpose Arrays serve in Ruby. They let us organize data so that each item holds a definite position or an index. The index allows us to access or modify each item individually.

Working with Arrays is as simple as this:

Ruby
1# Creating an array 2fruits = ['apple', 'banana', 'cherry'] 3 4# Add a new element at the end using push 5fruits.push('date') # ['apple', 'banana', 'cherry', 'date'] 6 7# Adding an element at the end using << 8fruits << 'elderberry' # ['apple', 'banana', 'cherry', 'date', 'elderberry'] 9 10# Inserting an element at a specific position 11fruits.insert(1, 'bilberry') # ['apple', 'bilberry', 'banana', 'cherry', 'date', 'elderberry'] 12 13# Removing all occurrences of a particular element 14fruits.delete('banana') # ['apple', 'bilberry', 'cherry', 'date', 'elderberry'] 15 16# Accessing elements using indexing 17first_fruit = fruits[0] # apple 18last_fruit = fruits[-1] # elderberry 19 20# You can also use .first and .last for similar results 21first_fruit_alt = fruits.first # apple 22last_fruit_alt = fruits.last # elderberry

Note that both push and << can be used to add elements to the end of an array. Use << for quick, single additions and push when adding multiple elements at once.

Understanding Strings

Think of Strings as a sequence of letters or characters. So whether you're writing down a message or noting a paragraph, it all boils down to a string in Ruby. Strings are enclosed by either single or double quotes.

Ruby
1# Creating a string 2greeting_string = "Hello, world!" 3 4# Accessing characters using indexing 5first_char = greeting_string[0] # 'H' 6last_char = greeting_string[-1] # '!' 7 8# Making the entire string lowercase 9lowercase_greeting = greeting_string.downcase # 'hello, world!'

In Ruby, strings are mutable, meaning we can alter them directly, as shown previously. However, many string methods return modified copies instead of altering the original string directly. Examples include downcase, upcase, strip, etc.

Indexing and Common Operations

Both arrays and strings allow us to access individual elements through indexing. In Ruby, we start counting from 0, meaning the first element is at index 0, the second at index 1, and so on. Negative indexing begins from the end, with -1 denoting the last element.

We have many operations we can perform on arrays and strings. We can slice them, concatenate them, repeat them, and even find the occurrence of a particular element!

Ruby
1# Define an array and a string 2my_array = [1, 2, 3, 4, 5] 3my_string = "Hello" 4 5# Slicing: my_array[start, length] 6slice_array = my_array[2, 2] # [3, 4] 7slice_string = my_string[1, 2] # "el" 8 9# Concatenation: my_array + another_array 10concatenate_array = my_array + [6, 7, 8] # [1, 2, 3, 4, 5, 6, 7, 8] 11concatenate_string = my_string + ", world!" # "Hello, world!" 12 13# Repetition: my_array * n 14repeat_array = my_array * 2 # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] 15repeat_string = my_string * 2 # "HelloHello" 16 17# Finding the first occurrence of an element in an array or a string 18found_1_in_array = my_array.include?(1) # true 19found_8_in_array = my_array.include?(8) # false 20found_in_string = my_string.downcase.include?('l') # true 21index_1_in_array = found_1_in_array ? my_array.index(1) : -1 # 0 22index_8_in_array = found_8_in_array ? my_array.index(8) : -1 # -1 23index_in_string = found_in_string ? my_string.downcase.index('l') : -1 # 2 24 25# Sorting items in an array 26sorted_array = my_array.sort.reverse # [5, 4, 3, 2, 1]

And there we have it, a Ruby toolkit for working with arrays and strings!

Lesson Summary and Practice

Give yourself a pat on the back! You've navigated through Ruby collections, primarily focusing on Arrays and Strings, learning how to create, access, and manipulate them via various operations.

Up next, reinforce your understanding with plenty of hands-on practice. Understanding these concepts, combined with frequent practice, will enable you to tackle more complex problem statements with ease. Happy coding in Ruby!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.