Coder Social home page Coder Social logo

my-collect'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.

my-collect's People

Contributors

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

my-collect's Issues

instructions do not match lab

The instructions in the readme file talk about making a method that will take a list of students' full names and return an array of just their first names. However, the lab's spec test file wants you to make a method that will take a list of programming language names in lower case and return an array of the programming language names in upper case.

IDE not working

the IDE is not running code here - other labs still working but doesn't run fully

Glitch

I believe there's a glitch within the last question of the Enumerators Code Challenge quiz (section 7, module 5) When I test 3 out of the 4 answers in ruby, all return true however, my answer was marked wrong. A screenshare coach couldn't give me an answer. Can IT review this and advise?

Thank you

discrepancies in my_collect_spec.rb

I came across a few discrepancies in the spec file that was cloned when i clicked the open button in this exercise. I was given the following spec file:

describe "my_collect" do
  let(:languages) { ['ruby', 'javascript', 'python', 'objective-c'] }
  let(:students) { ['Tim Jones', 'Tom Smith', 'Sophie Johnson', 'Antoin Miller'] }

  it "can handle an empty collection" do
    empty_array = []
    counter = 0
      my_collect(empty_array) do |x|
        counter += 1
      end
    expect(counter).to eq(0)
  end

  it "yields the correct element from a given collection, in this case languages" do
    my_collect(languages) do |language|
      expect(language).to_not eq(nil)
    end
  end

  it "returns a new collection of appropriately modified elements, in this case capitalized languages" do
    expect(my_collect(languages) do |language|
      language.upcase
    end).to eq(["RUBY", "JAVASCRIPT", "PYTHON", "OBJECTIVE-C"])
  end

  it 'does not modify the original collection' do
    my_collect(languages) do |language|
      language.upcase
    end
    expect(languages).to eq(['ruby', 'javascript', 'python', 'objective-c'])
  end

  it "yields the correct element from the given collection, in this case students" do
    my_collect(students) do |student|
      expect(student).to_not eq(nil)
    end
  end

  it "returns a new collection of appropriately modified elements, in this case student first names" do
    expect(my_collect(students) do |language|
      student.split(" ").first
    end).to eq(["Tim", "Tom", "Sophie", "Antoin"])
  end

  it 'does not modify the original collection' do
    my_collect(students) do |language|
      language.upcase
    end
    expect(students).to eq(['Tim Jones', 'Tom Smith', 'Sophie Johnson', 'Antoin Miller'])
  end
end

The solution provided in this exercise's tree does not line up with the spec file that the learn button gave to me.

I also came across an unused variable in the second my_collect(students) block:

it "returns a new collection of appropriately modified elements, in this case student first names" do
    expect(my_collect(students) do |language|
      student.split(" ").first
    end).to eq(["Tim", "Tom", "Sophie", "Antoin"])
  end

This block initially didn't allow my code to pass. I went ahead and changed |language| to |student| on my end which allowed student.split(" ").first to pass.

I'm not sure if this was intentional, but it helped me understand spec files more!

my-collect lab

I seem to be having an issue submitting my lab. I just need help figuring out how to solve this.
Thanks!

errors in rspec file

line 40 passed variable should be student not languages
line 41 string variable should be student not students

Bug in spec file?

Hi Flatiron,

Lines 40 and 41 of my_collect_spec.rb read:

expect(my_collect(students) do |language|
  student.split(" ").first

I think it should be:

expect(my_collect(students) do |student|
  student.split(" ").first

I changed it and it got the test to pass. I hope I didn't just cheat my way through the lesson. haha

won't return new arrays, only originals

`keeps returning the original array

def my_collect(array)
i = 0

while i < array.length
yield (array[i])
i = i + 1
end
return array
end

names = ["Tim Jones", "Tom Smith", "Jim Campagno"]
my_collect(names) do |x|
x.split(" ").first

end

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

end`

broken Rspec

my-collect-001-prework-web/spec/my_collect_spec.rb on line 40 the block should except student variable instead of language in order to make the test working according to the requirements.

Error in test file

Looks like there's an error in the test file that's causing it to fail even when the method is written correctly. Lines 39 - 43 in my_collect_spec.rb are:

it "returns a new collection of appropriately modified elements, in this case student first names" do
expect(my_collect(students) do |language|
student.split(" ").first
end).to eq(["Tim", "Tom", "Sophie", "Antoin"])
end

Instead of |language| it should be |student| - I made this change and the test passed

error in spec/my_collect_spec.rb

On line 40 of this spec:

expect(my_collect(students) do |language| student.split(" ").first

This creates an unneccessary NameError while testing the code. It should read

expect(my_collect(students) do |student| student.split(" ").first

Thanks,

Brad Watson

my_collect_spec.rb Issue

In the my_collect_spec.rb file, on line 40 (in the "returns a new collection of appropriately modified elements, in this case student first names" section) it says:

expect(my_collect(students) do |language|

I think it should be changed to:

expect(my_collect(students) do |student|

Otherwise "student" is undefined in the rest of the code (lines 41-43):

student.split(" ").first
    end).to eq(["Tim", "Tom", "Sophie", "Antoin"])
  end

clean up readme

remove front matter, added better instructions.

for example they should build this using yield and not .each

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.