Remove the First and Last Element from a Ruby Array
In this Ruby coding exercise, we are going to build out a method that removes the first and the last elements from an array.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now, this is a relatively simple exercise to build out if you're familiar with Ruby. If you're not then you're going to be able to learn quite a few things. And even if you have built out a number of Ruby programs I've thrown in a little bit of a twist on this exercise. The reason is because technically, this type of behavior is incredibly easy to build out. In fact, it just takes a few characters in order to do it.

You can see right here I have some rspec test cases


and let me just copy one of these and you're going to see exactly how easy it is to do. The goal here and you can look at the test cases to see what the goal is. If you have an array of elements, in this case, we have an array of string elements and so the first one is the string that has an H1 tag inside of it. The next one's just a string that says my content. The last one is the ending tag.


Now there's nothing specific about this being HTML content that is just kind of an example I wanted to use but you could do this with anything. And so what the goal is as you can see here in the expectation.


The goal is that when you run it through our method that it would return just my content. So it's going to strip out anything on the end of the array and so this is also the case when you have an array with any number of items so imagine you have this array and the first item is an empty string and then you have a couple other pieces of content that you want to keep and then the last one is just this little slash character and you want to get rid of the slash and the empty string. It really doesn't matter what is at the beginning or at the end, our goal is to be able to drop this.

So this is something that I've run into where I've had to build out something very similar to this where I've built out things like Web scrapers and anything where Id end up 100 percent of the time with content on both sides in an array that simply needs to be removed. Now technically this is something where all you'd have to do is do something like this where I can type in the name of the array and then inside of brackets say 1 ..-2.


And now if I want to test this out I can call this and you can see we get the exact behavior here on line 9 that we're looking for, so we get my content.


If we pass in any other elements so say I put something you know another spring here it doesn't really matter if I call this what's going to happen is it's just going to just take the very first element and the last element drop those off of the array and it's going to return anything in-between it whether you have 3 items or you have 3000 items it's going to have the same behavior. So technically to get the first part of this function working all we have to do is put in this line of code and then we change so instead of it saying the html array the argument we're passing in is an array. So that is all you'd have to do to get this working.


But and that takes care of this first test but as you can see here I wanted to throw in something to make this a little bit more of a robust method because imagine a scenario where you do not pass in at least three elements but instead you pass in no elements or one or two what are you going to do? What are you going to expect? What do you expect an empty array? Would you expect you know something that just says nill or anything like that, this could lead to some confusing bugs.

So in a case like this what I actually want is I want an error returned. Now an error in many times whenever you're running into them it may seem like a bad thing but when errors are built out and implemented properly an error is actually a very good thing an error is a way that the developer who created the program that you're using is trying to give you some guidance on how to properly use their program. And so what I want to do is I want to have a condition inside of our method so instead just taking any input I want it to raise an error if we pass in the wrong number of arguments.

I think it's a pretty practical expectation to believe that we should get at least three arguments in with our I shouldn't say three arguments. We are expecting just one argument of an array but that array should have at least three elements and if it does not have three then it should raise an error. So let's walk through how we can do that, if you want to see how you can test for this in rspect you can come down into this second test here and see how this works.

I'm gonna save this file and let's see exactly what the message is that we're getting from rspect so if say rspec may/28_spec.rb If I run this you can see we have two examples and one failure and the failure there says that we expected error to be raised but it wasn't.


That's what we need to build out. So inside of our method let me jump up to the top here, what I want to do is say raise standard error. And now if you want to get really detailed you could create your own error you could create a custom error class or something like that and to say though raise standard error just a basic one because I can pass in as a second argument here I can pass in whatever message it is that I'm looking for.

If you look down at the expectation the error should say at least three elements are needed in the array. So let me actually just jump down just so you don't have to see me copy in all of that and I'm going to just copy all this content and then at the very end here pace that in and now this should work after I add the conditions. I'm going to say I want you to raise this error if the array count is less than or equal to 2.

So this means that anything three or above that the program should work like normal and then anything that has two or a few or items in the array it should raise an error. We can come and test this out, so if I say HTML and I call our new methods remove first and last and pass in html. Now if I run this then this should work and yes we get another string in my content. So this is still working perfectly.


Now if we are to try to run this code with something else. So let me move a few items here so I'm just going to put in two items save this and switch down, because if I use my cool little seeing is believing process then it actually makes me restart the VIM and so I'm not going to do that. So let's just call the file itself so say ruby may/28_spec.rb run this and you can see that we have an error here and so it says remove first and last.


And so this is not going to work in the current value and in this case it's not actually printing out our description because we're not really calling it explicitly. But now if we call rspec so we run this so I'm gonna say May 28 and run this and let's see it looks like we have zero examples zero failures. And we have one error occurred outside the examples so let's see exactly what is happening here.


Okay, it says an error occurred while loading and this is actually fine, it's the error because we're calling this value. Let's get rid of our test code here, just so we can get all of our tests to green, and run rspec one more time. And there we go we have two examples and no failures.


So anytime you're a standard error it's going to stop the execution of the entire program including your tests. This is the way that you can build this out and as you can see this is a much better cleaner way of being able to build the program. Imagine that you built this in to a ruby gem or something like that. You do not want this to fail silently. So if you built out a method like this and someone else was using it and then it just returned nil for some unknown reason when they called it.

That would be a bad way of building the program because you never know that may happen to them in production and then they're really not going to be a fan of yours because you should have given them much better guidance here. So if you have some type of requirement in your code that you need run such as having a certain number of items in an array like we have right here then it's perfectly fine to raise an error and then give some guidance on how to fix it.

If you notice whenever you're building out a Rails application or a javascript application whenever you're not using the framework correctly Ill give you an error if you try to call a method with too few arguments or anything like that then it is going to raise an error and the error is not a bad thing. The error is telling you how you can go fix your program so when ever you're building out your own programs then you want to be able to raise errors when they're not being used properly and then give guidance on how to do it, just like we did.

If I were using a program and it gave me an error that said at least three elements are needed in the array. The very first thing I would do is I'd go look at my implementation of their program and see that I did not include enough elements in the array and then I would go be able to fix it and get the program working properly.