Get the Average from an Array in Ruby
In this Ruby coding exercise, we are asked to build out a get average method here in Ruby.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So what the goal of this is our method should be able to take an array. And so this array of either integers or floats would be passed in and then what our method needs to do is to take all of those and then return whatever the average is. And so you can see from our rspec example right here that if we passed in an array with the integers 1 through 6 then we should end up with a value of three point five which is the basic high-level average for those numbers.

large

Before we even get into building out this implementation let's kind of write out what this should look like. And part of the reason why I'm doing this is because if you're going through these exercises because you are preparing for a coding interview or anything like that, remember that whoever you are working with doesn't just want to see you get the right solution. They also want to see the way that you think through problems. And so whenever I'm presented with a new challenge what I like to try to do something like this where I write out some of the basic things that need to get done in order for this to work.

So as a comment here I know that I have an array and I know that array of numbers is going to have to be added up. So the first step will be to sum the elements from the array and then after that, I need to take that sum and then I need to take it and then divide the entire set of numbers because that's how we get an average. So I want to divide the sum by the total number of array elements.

large

So with those two things in place, we can build out this solution. Now the very first thing that we need to do is to add up and to sum up all the elements inside that array and the way we can do that is with the inject method. So if I have some array like this where I have 1, 2, and 3 I can call inject on that so I can say inject and there are a few ways you can write this. You can say inject like this do and then you would pass in whatever that sum is, that accumulator, and then whatever the element is. And then from there you would just say sum and then just add whatever the value of the element is to that. And so now if you ran this then you would get 6 which is exactly the behavior we want.

large

But thankfully we also have another way of writing out this kind of process where I can say 1, 2, and 3 say inject and then from there I can pass a block argument and you can do that by saying the ampersand followed by a colon followed by plus.

[1, 2, 3].inject(&:+)

And what this is telling Ruby is that this inject method takes a block like we can see right here we're passing a block to the method but instead of going through the entire process of creating the do and the n block and adding the accumulator and the element what inject does is when you pass it a block like this it makes some assumptions. It assumes that you want to start your value at zero which is exactly what we have right here. And then it also assumes that whatever process or method that you call will simply be called on top of that and it's going to accumulate whatever that value is.

So what I have from lines and 9 through 11 and what I have on line 13 is completely identical. And we can test this out by running the ruby code right there and as you can see this adds it up the exact same way.

large

So I'm going to use that code so I'll say array dot inject and then I'm simply going to pass in that block of plus and so this is going to give us our sum. Now let's test this out, let's get rid of that example and I'm also going to get rid of this code. And now if I say get average and pass that in we should get our sum. And there we go, so that is working.

large

Now we have crossed this one off of the list so we can say that that one's done we've summed up the elements from the array. The next thing we need to do is to divide the sum by the total number of elements so we can add onto this line right here and just say I want you to take that sum and then divide it by array.size. Now this is not going to be the final solution as you're going to find out here in a second. So if I run this, this gives us 2. But there's a little bit of a problem and I'm going to show it to you because it's one that is a silent one. And so I want to take in all of those examples so 1 through 6 and let me save that and run it again. And you can see it tells us our average is 3 which is not actually accurate, remember our average should be 3.5.

The problem is because whenever you have these kinds of numbers and you're working with integers in ruby and you perform a division like we're doing here. What Ruby does is it assumes that you do not want decimals returned, you don't want a float value returned. So we can fix that by just adding on and casting our size to a float. So I can just say to_f and save that and now if I run that code you can see that get_average returns 3.5.

large

So what it's doing here is it's taking that size of the array and so in the case of our example where it's 6, instead of it looking like this. So when we originally before we add to_f what it looked like is it grabbed the sum of all those values and then it said I want to divide this by 6. But what we're doing here when we cast it to a float we say 6.0 that's what this line of code right here, that's what this is doing. And so whenever you are using this for division then Rubys going to assume that you want a float value back and that's a reason why that worked.

So let's get rid of our test code here save everything. Let's run our rspec code just to make sure it's all working. So I have this listed as May 26. So if I run this with one example zero failures so this is working perfectly.

large

So great job if you went through that you now know how to get the average from an array in Ruby.

Resources