Man With Code 👨‍💻

In which I occasionally teach you things.

Programming With Ruby Episode 7, Arrays

Series: Ruby Programming

Covered In This Episode: 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: [sourcecode language="ruby"] x = [] [/sourcecode] This is an empty array, if we wanted an array with something in it: [sourcecode language="ruby"] todo_list = ["Cut Grass", "Buy Food", "Fix Tom's Computer"] [/sourcecode] 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: [sourcecode language="ruby"] todo_list[0] #=> "Cut Grass" [/sourcecode] You can also add items to an array in a similar way [sourcecode language="ruby"] todo_list[3] = "Go Skydiving" [/sourcecode] Another way to add items is to use +=, which you may recognize from previous tutorials [sourcecode language="ruby"] todo_list += ["Eat Sandwich"] [/sourcecode] 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: [sourcecode language="ruby"] todo_list[0..2] #=> ["Cut Grass", "Buy Food", "Fix Tom's Computer"] [/sourcecode] 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: [sourcecode language="ruby"] todo_list[3..-1] #=> ["Go Skydiving", "Eat Sandwich"] [/sourcecode] Array Iteration If you want to loop over each element of an array you use the each method: [sourcecode language="ruby"] numbers = [1, 2, 3, 4] numbers.each do |number| puts number * 2 end #=> 2 4 6 8 [/sourcecode] You can do the same thing, but turn the output into an array with the collect method: [sourcecode language="ruby"] numbers = [1, 2, 3, 4] numbers.collect do |number| number * 2 end #=> [2,4,6,8] [/sourcecode] 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 protected] Thanks for watching, bye!

Comments

Akash on

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 :)

Tyler on

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: <code>twice = numbers.collect do |number| number*2 end</code> And then the variable twice would hold [2,4,6,8]. And indeed you are correct, I'll fix the transcript. Thanks for that! :)