首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Monkey和RSpec

Monkey和RSpec
EN

Stack Overflow用户
提问于 2014-10-10 06:02:19
回答 1查看 903关注 0票数 0

我在我的课程中处于块检查点,我必须让它通过rspec才能继续前进。猴子补丁(到目前为止我讨厌它,笑)站在我的方式完成块,我知道它是错误的,但我会张贴我所拥有的,也许我可以最终提交这一个,然后转移到每个索引。这是我几周前得到的,我相信这太复杂了。

首先,这是规格

代码语言:javascript
复制
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

下面是我输入的内容:

代码语言:javascript
复制
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

到目前为止,我只能让字符串折叠,去掉它们的空格,字符串折叠不会变异为传递

EN

回答 1

Stack Overflow用户

发布于 2014-10-10 07:58:11

收到了帮助,并传递了以下内容:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26288645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档