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.