0

我有这个代码(在撬):

class Hash
  def invert_by_array
    each.with_object({}) {|(key,value), obj|
      value.each do |element|
        obj[element] = key
      end
    }
  end
end

require 'minitest/spec'

describe "invert_by_array" do
  it "should use the array element as keys" do
    {'foo' => %w(bar baz)}.invert_by_array.must_equal({'bar' => 'foo', 'baz' => 'foo'})
  end
end

如何从编辑底部运行测试 - 基本上将上面的内容复制/粘贴到 readline?

4

1 回答 1

0

您可以通过调用来调用跑步者MiniTest::Unit.runner.run

class Hash
  def invert_by_array
    each.with_object({}) {|(key,value), obj|
      value.each do |element|
        obj[element] = key
      end
    }
  end
end

require 'minitest/spec'

describe "invert_by_array" do
  it "should use the array element as keys" do
    {'foo' => %w(bar baz)}.invert_by_array.must_equal({'bar' => 'foo', 'baz' => 'foo'})
  end
end

MiniTest::Unit.runner.run
于 2012-08-03T14:55:14.303 回答