Git Product home page Git Product logo

my-collect-seattle-web-082619's Introduction

My #collect

Objectives

  1. Build a method that yields members of a collection to a block.
  2. Control the return value of a method that uses yield such that it returns a new collection.

Instructions

You'll be writing your code in lib/my_collect.rb.

You are writing a method that behaves just like the real #collect method. It should take in an argument of a collection, iterate over that collection using a while loop, and execute the code in the block you call it with for each element in the collection (use the yield keyword). It should return the modified collection.

Your #my_collect method, therefore, should not care about the contents of the code block that it is invoked with. For example, let's say we are writing an app to help teachers manage their students. Our teacher has a list of students:

["Tim Jones", "Tom Smith", "Jim Campagno"]

The list includes the first and last name of each student, but our teacher needs to collect a list of just their first names.

So, if our teacher uses #my_collect to collect the first name of his students, it should work like this:

array = ["Tim Jones", "Tom Smith", "Jim Campagno"]
my_collect(array) do |name|
  name.split(" ").first
end

Should return:

["Tim", "Tom", "Jim"]

What if your method was being invoked with a totally different collection and a totally different code block? For example, let's say your #my_collect method is given an argument of a list of programming languages and passed a block that capitalizes the elements yielded to it.

collection = ['ruby', 'javascript', 'python', 'objective-c']
my_collect(collection) do |lang|
  lang.upcase
end

# => ["RUBY", "JAVASCRIPT", "PYTHON", "OBJECTIVE-C"]

Your method should behave the same way––iterating over the given collection and passing each member to the given block––regardless of the content of the collection or the nature of the block.

Make sure to run bundle install before running your tests.

View My `#collect` on Learn.co and start learning to code for free.

my-collect-seattle-web-082619's People

Contributors

sophiedebenedetto avatar deniznida avatar gj avatar sgharms avatar maxwellbenton avatar annjohn avatar fs-lms-test-bot avatar pletcher avatar dakotalmartinez avatar danl12186 avatar irmiller22 avatar jmburges avatar sarogers avatar fislabstest avatar ultimatecrispy avatar ahimmelstoss avatar ga-be avatar joshuabamboo avatar markedwardmurray avatar

Watchers

Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar  avatar Joe Cardarelli avatar Sara Tibbetts avatar  avatar  avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Scott Ungchusri avatar Nicole Kroese  avatar Lore Dirick avatar Kaeland Chatman 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.