当我在irb中运行以下内容时,它会返回我想要的内容。但是当我运行rspec -c intersection_spec.rb
时,它会返回[[0,0]]
。为什么我没有用rspec获得所需的结果?
我在这里做错了什么?
intersection.rb
class Intersection
def self.create_arr(xa1, ya1, xa2, ya2)
((xa1.to_i)..(xa2.to_i)).to_a.product(((ya1.to_i)..(ya2.to_i)).to_a)
end
end
intersection_spec.rb
require './spec_helper'
require './intersection.rb'
describe Intersection do
@xa1 = 0.0
@ya1 = 0.0
@xa2 = 5.0
@ya2 = 5.0
@xb1 = 1.0
@yb1 = 1.0
@xb2 = 4.0
@yb2 = 4.0
specify{ expect(Intersection.create_arr(@xa1, @ya1, @xa2, @ya2)).to eq [[0,0],
[0,1], [0,2], [0,3], [0,4], [0,5], [1,0], [1,1], [1,2], [1,3], [1,4], [1,5],
[2,0], [2,1], [2,2], [2,3], [2,4], [2,5], [3,0], [3,1], [3,2], [3,3], [3,4],
[3,5], [4,0], [4,1], [4,2], [4,3], [4,4], [4,5], [5,0], [5,1], [5,2], [5,3],
[5,4], [5,5]] }
end
运行rspec。
rspec -c intersection_spec.rb
expected: [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [1, 0], [1, 1],
[1, 2], [1, 3], [1, 4], [1, 5], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5],
[3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [4, 0], [4, 1], [4, 2], [4, 3],
[4, 4], [4, 5], [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]
got: [[0, 0]]
在irb
irb(main):029:0> @xa1 = 0.0
=> 0.0
irb(main):030:0> @ya1 = 0.0
=> 0.0
irb(main):031:0> @xa2 = 5.0
=> 5.0
irb(main):032:0> @ya2 = 5.0
=> 5.0
irb(main):033:0> @xb1 = 1.0
=> 1.0
irb(main):034:0> @yb1 = 1.0
=> 1.0
irb(main):035:0> @xb2 = 4.0
=> 4.0
irb(main):036:0> @yb2 = 4.0
=> 4.0
irb(main):037:0> def self.create_arr(xa1, ya1, xa2, ya2)
irb(main):038:1> ((xa1.to_i)..(xa2.to_i)).to_a.product(((ya1.to_i)..(ya2.to_i)).to_a)
irb(main):039:1> end
=> nil
irb(main):040:0> create_arr(@xa1, @ya1, @xa2, @ya2)
=> [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [1, 0], [1, 1], [1, 2],
[1, 3], [1, 4], [1, 5], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [3, 0],
[3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4],
[4, 5], [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5]]
答案 0 :(得分:0)
我无法确定为什么它会失败,但我注意到当你没有制作初始坐标实例变量时它确实有效,所以
@xa1 = 0.0
@ya1 = 0.0
@xa2 = 5.0
@ya2 = 5.0
成为
xa1 = 0.0
ya1 = 0.0
xa2 = 5.0
ya2 = 5.0
然后将它们传递给#create_array。