2

Trying to define a new_map function that takes an array as an argument and return a new array modified according to the following RSpec:

describe "new_map" do
  it "should not call map or map!" do
    a = [1, 2, 3]
    a.stub(:map) { '' }
    a.stub(:map!) { '' }

    expect( new_map(a) { |i| i + 1 } ).to eq([2, 3, 4])
  end

  it "should map any object" do
    a = [1, "two", :three]
    expect( new_map(a) { |i| i.class } ).to eq([Fixnum, String, Symbol])
  end
end

My code works independently, but cannot satisfy both RSpec simultaneously. I understand I have two methods with the same name(new_map), but I don't know how to combine the two.

def new_map(array)
  new_array = []
  array.each do |item|
    new_array << item +1
  end
  new_array
end

def new_map(array)
  new_array = []
  array.map do |item|
    new_array << item.class
  end
  new_array
end

Thanks for helping a beginner.

0

1 Answer 1

3

This should work :

def new_map(array)
    new_array = []
    array.each do |item|
        new_array << yield(item)
    end
    new_array
end

EDIT : Btw, the thing that causing troubles was that the specific way you want to map list items was contained inside the method. I think changing name of one of the two is the only way to keep it working like that.

Also you are giving blocks while calling new_map(), but not using them from inside new_map().

Inside the function, the keyword 'yield' allows you to call the block passed to your function, and also provide any values if any. So the right idea is to separate the exact mapping stuff inside a block, and from within the function call it with iteratively with each element of the list, then return the results to an array you want to return.

EDIT2 : I also want to add that if you want a special map function with a particular mapping method only, I would recommend using a reusable block created with proc instead. Example :

array = [1,2,3]

# Create the mapping you want.
mapping = proc do |item|
    item + 1
end

# Now use that proc in a map function.
new_map(array, &mapping)
# => [2,3,4]
Sign up to request clarification or add additional context in comments.

3 Comments

The tests are using blocks. The method should work fine as is.
limekin, thanks. I guess I didn't understand that the Spec wasn't calling for a literal use of "map". Also, I am still trying to understand what needs to be included in the method vs what is included when it is called.
Yeah, the test or question aims to give the solver an idea of how the usual 'map' is implemented. Btw, are you familiar with passing blocks into methods and calling them from inside the method ? If not, knowing that well will make things easier for you on deciding what to include inside the method and while calling the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.