Runnin' this site ain't free, help us out! Donate.

Programming With Ruby Episode 7, Arrays

by Tyler on June 21st, 2009
Programming With Ruby Episode 6, StringsProgramming With Ruby Episode 8, Hashes

Covered In This Episode:

  • What Aarrays Are
  • Array Creation
  • Accessing Arrays
  • Array Iteration
  • Other Array Methods

Transcript:

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
have.

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
list.

Because computers start counting at 0, the first element in the
array is 0, instead of one.

Array Creation

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.

Accessing Arrays

Now that you have created an array, how do you go about accessing each
item?

I told you earlier that each item had a number, so to access the first
item in the array you would do:

todo_list[0] #=> "Cut Grass"

You can also add items to an array in a similar way

todo_list[3] = "Go Skydiving"

Another way to add items is to use +=, which you may recognize from
previous tutorials

todo_list += ["Eat Sandwich"]

Don’t forget that if you use += that the item your adding has to be
between brackets

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"]

Array Iteration

If you want to loop over each element of an array you use the each
method:

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
collect method:

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 tyler@manwithcode.com

Thanks for watching, bye!

Programming With Ruby Episode 6, StringsProgramming With Ruby Episode 8, Hashes

Be Sociable, Share!

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

2 Comments
  1. Akash permalink

    A couple of questions and corrections.

    When you do this:

    numbers=[1,2,3,4]
    numbers.collect do |number|
    number*2
    end

    The output that is given is 2,4,6,8 which is fine. Where is this array stored though? The numbers array doesnt change, and “number” doesn’t seem to be an array either.

    Also you forgot to include “.collect” in the Transcript :)

    • The new array is the return value of the #collect method. Since we don’t do much with that return value, the new array is simply displayed in IRB. To put this new array into a variable, we could just add something like:

      twice = numbers.collect do |number|
      number*2
      end

      And then the variable twice would hold [2,4,6,8].

      And indeed you are correct, I’ll fix the transcript. Thanks for that! :)