Giter Site home page Giter Site logo

programming-univbasics-4-array-simple-array-manipulations-part-1-nyc01-seng-ft-091420's Introduction

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

In this lab, your task is to create methods that use some of the common Array methods. 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

Create the methods detailed below in lib/intro_to_simple_array_manipulations.rb. Let's work through the first together!

using_push

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

To start, we know the method name, and that it takes in two parameters. We can name theese parameters array and string for simplicity.

def using_push(array, string)

end

With the method created, we can add the implementation. As we saw in the previous example, we can append .push to any array, pass in a value, and that value will get added to the array.

def using_push(array, string)
  array.push(string)
end

Calling array.push(string) will return the updated array, which happens to be what we using_push shoulds return. The rest are up to you solve!

using_unshift

This method takes in two parameters, 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 parameter 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 a parameter of an Array and uses the .pop method. This time, however, pass an argument, 2, into .pop to remove the last two array items and return them.

using_shift

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

shift_with_args

This method takes in a parameter of an Array and uses the .shift method. This time, pass an argument, 2, into .shift to remove and return the first two items from the array.

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

programming-univbasics-4-array-simple-array-manipulations-part-1-nyc01-seng-ft-091420's People

Contributors

maxwellbenton avatar jenmyers avatar drakeltheryuujin avatar

Watchers

 avatar  avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar  avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.