Only one array is returned where I am expecting array of array in Ruby -


when run following in irb, returns want. when run rspec -c intersection_spec.rb, returns [[0,0]]. why don't desired results rspec?

what doing wrong here? thank you in advance.

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    @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 

run 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]] 

in 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]] 

i can't sure why fails way, noticed work when don't make initial coordinates instance variables, so

  @xa1 = 0.0   @ya1 = 0.0   @xa2 = 5.0   @ya2 = 5.0 

becomes

  xa1 = 0.0   ya1 = 0.0   xa2 = 5.0   ya2 = 5.0 

then pass #create_array.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -