0% found this document useful (0 votes)
37 views12 pages

Working With Ruby Strings & Arrays - RubyGuides

The document discusses different ways to work with and store data in Ruby including strings, arrays, and hashes. It provides examples and explanations of string, array, and hash methods and how to use them to manipulate and retrieve data.

Uploaded by

saddestmonke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

Working With Ruby Strings & Arrays - RubyGuides

The document discusses different ways to work with and store data in Ruby including strings, arrays, and hashes. It provides examples and explanations of string, array, and hash methods and how to use them to manipulate and retrieve data.

Uploaded by

saddestmonke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

Working With Ruby


Strings, Arrays &
Hashes

https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 1/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 2/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

This is lesson #3 of the Complete Ruby Tutorial For Beginners series.


You'll learn about different ways to work with & store data in Ruby.

1 Learning Ruby

2 Understanding Variables

3 Working With Collections

4 If / Else Conditional Statements

5 Ruby Loops: Repeating Something


Many Times

6 Thinking Like A Programmer

7 Object-Oriented Programming

 Download eBook

What Is a String?
You’ve learned about variables & basic math operations in Ruby.

Next you’re going to learn about strings.


https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 3/12
Next you re going to learn about
03/04/2024, 12:11
strings.
Working With Ruby Strings & Arrays - RubyGuides

A string is a sequence of characters inside quotes.

Example:

1. "bacon"

You need the quotes so you can tell the difference between
strings & variables.

A string is data.

A variable is a name for something.

In fact, you can use a variable to name a string:

1. food = "bacon"

This helps you reuse the string as many times as you want.

What Can You Do With Strings?


Strings can do many things for you.

For example, you can ask a string for its size:

1. "bacon".size
2.
3 # 5
https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 4/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides
3. # 5

Or you can tell the string to uppercase itself:

1. "bacon".upcase
2.
3. # "BACON"

We call these “methods”, and there are many of them.

There are methods that allow you to substitute all or part of the
string.

Like gsub :

1. "bacon".gsub("acon", "inary")
2.
3. # "binary"

Or to split the string into a list of characters:

1. "bacon".chars
2.
3. # ["b", "a", "c", "o", "n"]

Check this article for a more complete list of string methods.

When To Use Strings


You use strings whenever you want to print a message to the screen.
https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 5/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

Like this:

1. puts "Hello There!"

Also when you read files, read input from the user (with the gets
method), or when you want to combine several pieces of information
together.

Strings can contain numbers, but that doesn’t mean you can treat
them like numbers.

Here’s what I mean:

1. "1" + "1"

Gives you:

1. "11"

If you want to convert a string with numbers into an actual integer


value you have to use the to_i method.

Remember: A method is a command you can give to any object


in Ruby. Depending on the kind of object you’re working with
they will respond to different methods.

Converting a string to integer:


https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 6/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

1. "1".to_i

Converting an integer to a string:

1. 1.to_s

Why is there a difference?

Because integers are numbers, they must behave like numbers &
allow for mathematical operations.

Strings have a different purpose, and a different set of methods.

How to Combine Strings Using


String Interpolation
To combine numbers & strings you need a technique named “string
interpolation”.

Here’s an example:

1. age = 20
2. name = "David"
3.
4. puts "Hello #{name}, our records tell us that you're
#{age} years old!"

This is like a template.


https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 7/12
This is like a template.
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

Ruby replaces these #{name} & #{age} by their values, producing the
combined string.

How to Use Arrays


If you want to have many of the same thing then arrays are very
useful.

An array is a collection of items in a single location.

Here’s an example:

1. [1, 2, 3, 4, 5]

This is an array of integers.

You can access every element by its position.

We call that position an index.

Example:

1. letters = ['a', 'b', 'c']


2.
3. letters[0]
4. # 'a'
5.
6. letters[1]
7. # 'b'
https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 8/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

8.
9. letters[2]
10. # 'c'

Important!

The first element in an array is always on position 0.

Don’t ask me why, but you have to remember that.

If you ask for an index that is bigger than the array size you’ll get a nil
value.

It looks like this:

1. letters[4]
2.
3. # nil

A nil value is Ruby telling you:

“I can’t find what you want so here is a generic response”

And just like strings, arrays have a set of methods you can use to
make them do things.

For example:

1. letters.size
2.
3. # 3
https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 9/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

You can add new elements to an array like this:

1. numbers = []
2.
3. numbers << 1
4. numbers << 2
5. numbers << 3
6.
7. numbers
8. # [1, 2, 3]

This is a very useful array method, so write it down.

Both strings & arrays are very important building blocks for
writing your Ruby programs.

If you don’t understand how these 2 work you won’t be able to write
even the most basic projects you can think of.

Don’t just read this…

Go open irb now & practice.

If you don’t know what irb is or how to open it you need to go back to
chapter 1 of this guide.

How to Use a Ruby Hash


A hash is like a dictionary.

https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 10/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

It helps you match two values together…

…like a domain name to an IP address, or a phone number to a


person’s name.

Here’s how to create a hash in Ruby:

1. ip_to_domain = { "rubyguides.com" =>


"185.14.187.159" }

You can get the value for a hash key like this:

1. ip_to_domain["rubyguides.com"]
2.
3. # "185.14.187.159"

And you can change the value like this:

1. ip_to_domain["rubyguides.com"] = "8.8.8.8"

These are the 3 main operations you can do with a hash.

Btw, you use a comma to separate multiple key/value pairs.

Like this:

1. values = { "a" => 1, "b" => 2 }

You can find a list of hash methods here.


https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 11/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides

Do These Exercises Now


Create an array with the name of food with these values:
“bacon”, “orange”, “apple”
Access the first element of the food array
Access the last elements of the food array
Add a new element into the array: “yogurt”
Create a hash with 3 key/value pairs representing country
codes (like ES) & their country names (like Spain).
Answer this question: What is the difference between 1 and
"1" ?

 Last Lesson

Next Lesson 

Copyright RubyGuides.com

https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 12/12

You might also like