Runnin' this site ain't free, help us out! Donate.
Programming With Ruby Episode 7, Arrays
Covered In This Episode:
- What Aarrays Are
- Array Creation
- Accessing Arrays
- Array Iteration
- Other Array Methods
Hello everybody and welcome to Programming With Ruby Episode 7,
Arrays. I’m Tyler. And this video is brought to you by manwithcode.com
In this episode I will be telling you what arrays are, how you can
create an array in ruby. How to manipulate arrays by accessing them,
iterating over them, and by showing you a few useful methods they
Lets get started!
What are Arrays?
Arrays are a type of variable. Think of an array as a list. This list
can hold anything, names, numbers, objects, anything. Objects in the
array have a number, depending on what place they are in the
Because computers start counting at 0, the first element in the
array is 0, instead of one.
This is how a variable is defined in ruby:
x = 
This is an empty array, if we wanted an array with something in it:
todo_list = ["Cut Grass", "Buy Food", "Fix Tom's Computer"]
Each bracket represents the start and the end of the array,
respectively. Each item in an array is separated by a comma.
Now that you have created an array, how do you go about accessing each
I told you earlier that each item had a number, so to access the first
item in the array you would do:
todo_list #=> "Cut Grass"
You can also add items to an array in a similar way
todo_list = "Go Skydiving"
Another way to add items is to use +=, which you may recognize from
todo_list += ["Eat Sandwich"]
Don’t forget that if you use += that the item your adding has to be
You can also use ranges to access elements in the array. ranges are
used in a similar way that you normally access arrays, except ranges
look like this:
todo_list[0..2] #=> ["Cut Grass", "Buy Food", "Fix Tom's Computer"]
The 0 is the start number and the 2 is the end number. you can also
use -1, which is the end of the array:
todo_list[3..-1] #=> ["Go Skydiving", "Eat Sandwich"]
If you want to loop over each element of an array you use the each
numbers = [1, 2, 3, 4] numbers.each do |number| puts number * 2 end #=> 2 4 6 8
You can do the same thing, but turn the output into an array with the
numbers = [1, 2, 3, 4] numbers.collect do |number| number * 2 end #=> [2,4,6,8]
Other Array Methods
Now I’m going to show you some useful methods arrays have!
empty? tells you if an array is empty
sort sorts the array (alphabetically)
reverse reverses the array
delete deletes a specified item from the array
delete_at deletes whatever is at that index in the array
find finds an item with the specified value
inspect returns the way an array looks in code, instead of its values, this is useful for puts my_array.inspect
length how long the array is
That’s it for today’s episode
Please don’t forget to donate, the link should be to the right of this video
If you have any questions or comments, leave a comment on this page or
email me at email@example.com
Thanks for watching, bye!Programming With Ruby Episode 6, StringsProgramming With Ruby Episode 8, Hashes