2014-10-09 94 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