Working With Ruby Strings & Arrays - RubyGuides
Working With Ruby Strings & Arrays - RubyGuides
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
1 Learning Ruby
2 Understanding Variables
7 Object-Oriented Programming
Download eBook
What Is a String?
You’ve learned about variables & basic math operations in Ruby.
Example:
1. "bacon"
You need the quotes so you can tell the difference between
strings & variables.
A string is data.
1. food = "bacon"
This helps you reuse the string as many times as you want.
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
1. "bacon".upcase
2.
3. # "BACON"
There are methods that allow you to substitute all or part of the
string.
Like gsub :
1. "bacon".gsub("acon", "inary")
2.
3. # "binary"
1. "bacon".chars
2.
3. # ["b", "a", "c", "o", "n"]
Like this:
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.
1. "1" + "1"
Gives you:
1. "11"
1. "1".to_i
1. 1.to_s
Because integers are numbers, they must behave like numbers &
allow for mathematical operations.
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!"
Ruby replaces these #{name} & #{age} by their values, producing the
combined string.
Here’s an example:
1. [1, 2, 3, 4, 5]
Example:
8.
9. letters[2]
10. # 'c'
Important!
If you ask for an index that is bigger than the array size you’ll get a nil
value.
1. letters[4]
2.
3. # nil
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
1. numbers = []
2.
3. numbers << 1
4. numbers << 2
5. numbers << 3
6.
7. numbers
8. # [1, 2, 3]
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.
If you don’t know what irb is or how to open it you need to go back to
chapter 1 of this guide.
https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 10/12
03/04/2024, 12:11 Working With Ruby Strings & Arrays - RubyGuides
You can get the value for a hash key like this:
1. ip_to_domain["rubyguides.com"]
2.
3. # "185.14.187.159"
1. ip_to_domain["rubyguides.com"] = "8.8.8.8"
Like this:
Last Lesson
Next Lesson
Copyright RubyGuides.com
https://www.rubyguides.com/ruby-tutorial/working-with-ruby-collections/ 12/12