2014-10-09 117 views
0

我在我的課程塊檢查站,我必須讓它通過rspec爲了繼續前進。猴子補丁(到目前爲止我討厭它,哈哈)站在我完成塊的方式,我知道這是錯誤的,但我會張貼我有什麼,也許我可以最終提交這一個,然後移動到每個索引。這是我幾星期前的事情,我確信它太複雜了。猴子補丁和RSpec

首先這裏有規格

describe Array do 
    describe '#new_map' do 
    it "returns an array with updated values" do 
     array = [1,2,3,4] 
     expect(array.new_map(&:to_s)).to eq(%w{1 2 3 4}) 
     expect(array.new_map{ |e| e + 2 }).to eq([3, 4, 5, 6]) 
    end 

    it "does not call #map" do 
     array = [1,2,3,4] 
     array.stub(:map) { '' } 
     expect(array.new_map(&:to_s)).to eq(%w{1 2 3 4}) 
    end 

    it "does not change the original array" do 
     array = [1,2,3,4] 
     expect(array.new_map(&:to_s)).to eq(%w{1 2 3 4}) 
     expect(array).to eq([1,2,3,4]) 
    end 
    end 

    describe '#new_select!' do 
    it "selects according to the block instructions" do 
     expect([1,2,3,4].new_select!{ |e| e > 2 }).to eq([3,4]) 
     expect([1,2,3,4].new_select!{ |e| e < 2 }).to eq([1]) 
    end 

    it "mutates the original collection" do 
     array = [1,2,3,4] 
     array.new_select!(&:even?) 
     expect(array).to eq([2,4]) 
    end 
    end 
end 

describe String do 
    describe "collapse" do 
    it "gets rid of them white spaces" do 
     s = "I am a white spacey string" 
     expect(s.collapse).to eq("Iamawhitespaceystring") 
    end 

    it "doesn't mutate" do 
     s = "I am a white spacey string" 
     s.collapse 
     expect(s).to eq("I am a white spacey string") 
    end 
    end 

    describe "collapse!" do 
    it "mutates the original string" do 
     s = "I am a white spacey string" 
     s.collapse! 
     expect(s).to eq"Iamawhitespaceystring" 
    end 
    end 
end 

而且這是我輸入的內容:

class Array 
    def new_map(&block) 
    self.replace(self.map(&block)) 
    end 

    def new_select!(&block) 
    self.replace(self.map(&block)) 
    #[1,2,3,4].new_select!{ |e| e > 2 })=(&block) 
    end 
end 

class String 
    def collapse 
    s = "I am a white spacey string".delete(' ') 


    end 

    def collapse! 

    s.delete('+') 

    end 

end 

到目前爲止,我只能夠得到字符串崩潰擺脫他們的空格和字符串崩潰不會突變通過

回答

3

收到的幫助已通過:

class Array 
    def new_map 
    new_array = [] 
    each do |item| 
     new_array << yield(item) 
    end 
    new_array 
    end 

    def new_select!(&block) 
    replace(select(&block)) 
    end 
end 

class String 
    def collapse 
    split.join 
    end 

    def collapse! 
    replace(collapse) 
    end 
end