atomicjuice / programming-univbasics-4-array-simple-array-manipulations-london-web-111819

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction To Simple Array Manipulations

Learning Goals

  • Identify the result of method calls on arrays
  • Perform simple array manipulations

Introduction

In a previous lab, you learned how to create arrays and access the elements of an array. Now you are ready to manipulate arrays using different Ruby methods.

Fork and clone this lab. Run learn to see the initial test failures, then write the required methods in the lib/introduction_to_simple_array_manipulation.rb file to get the tests to pass.

Consult the official documentation of the Ruby Language for the methods you need to use to solve the problems in this lab.

Identify the Result of Method Calls on Arrays

As you read the method descriptions, make sure to pay attention to understanding what the method returns. For instance, when you call .push on an array, it returns the updated array:

artists = ["Hozier", "Ariana Grande", "Usher"]
next_artist = "Beyonce"
artists.push(next_artist)
# => ["Hozier", "Ariana Grande", "Usher", "Beyonce"]

However, .pop works a little differently; it removes (and returns) the last element from an array:

artists = ["Frida Kahlo", "Pablo Picasso", "Cai Guo-Qiang"]
artists.pop
# => "Cai Guo-Qiang"

artists
# => ["Frida Kahlo", "Pablo Picasso"]

You can see that while .pop removes "Cai Guo-Qiang" from the artists array, .pop also supplies "Cai Guo-Qiang" as its return.

Top-tip: One of the most common problems you will encounter on your journey to becoming a software developer is not knowing what object a method returns. For example, you might assume that the result of a method call is a string but instead it is an array.

Perform Simple Array Manipulations

using_push

This method takes in two arguments, an Array and a String. It adds that string to the end of the array using the .push method.

using_unshift

This method takes in two arguments, an Array and a String and adds that string to the front of the array using the .unshift method.

using_pop

This method takes in argument of an Array and uses the .pop method to remove the last element from the array and return that element.

pop_with_args

This method takes in an argument of an Array and uses the .pop method with an argument of 2 to remove the last two array items and return them.

using_shift

This method takes in an argument of an Array and uses the .shift method to remove the first item and return it.

shift_with_args

This method takes in an argument of an Array and uses the .shift method with an argument of 2 to remove and return the first 2 items from the array.

using_concat

This method takes in two arguments of two different arrays and uses the .concat method to add the contents of the second array to the first.

using_insert

This method takes in two arguments, an Array and a new element to be added to the array. it uses the .insert method to add the new element to the 4th index of the array.

using_uniq

This method takes in an argument of an Array and uses the .uniq method to remove any duplicate items.

using_flatten

This method takes in an argument of an Array that contains other arrays and uses the .flatten method to return an array of strings.

using_delete

This method takes in two arguments, an Array and a String, and uses the .delete method to remove any items from the array that are equal to that string.

using_delete_at

This method takes in two arguments, an Array and an Integer and deletes the element at the index of the array that is equal to that integer.

Conclusion

Now that you've gotten familiar with a few of the methods that the Ruby library provides, you can perform some of the same manipulations you've been practice much more simply.

Resources

About

License:Other


Languages

Language:Ruby 100.0%