Reputation: 15
My Code Runs When I Test It Within The Program, But When I Try To run Rspec on The Code It Throws a Error
rails--version
Rails 5.2.3
ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x64-mingw32]
I have Tried Making The Variables Public And Also Removing Variables and writing the code in different ways
Test File
require 'sort'
describe Sorter do
let (:sort) { Sorter.new }
let (:array) {array[10,6,2,1,5,4,3,9,8,7]}
context "Sort Low To High" do
it "Goes From 1 - 10" do
expect(sort.sortup(array)).to eql([1,2,3,4,5,6,7,8,9,10])
end
end
context "Sort High To Low" do
it "Goes From 10 - 1" do
expect(sort.sortdown(array)).to eql([10,9,8,7,6,5,4,3,2,1])
end
end
end
Main File
class Sorter
def sortup array
j = 0
while j < array.length - 1
i = 0;
while i < array.length - 1
if(array[i] > array[i+1])
temp = array[i+1]
array[i+1] = array[i]
array[i] = temp
end
i += 1
end
j += 1
end
array
end
end
Failure/Error: Unable to find matching line from backtrace
SystemStackError:
stack level too deep
Upvotes: 0
Views: 214
Reputation: 369468
let (:array) {array[10,6,2,1,5,4,3,9,8,7]}
The way let
works is that essentially it defines a method whose name is the Symbol
you pass to let
and the body is the block. So, this is essentially equivalent to
def array
array[10,6,2,1,5,4,3,9,8,7]
end
As you can see, array
calls itself, and there is no condition for it to stop doing so, so you get endless recursion. The solution is simple: don't have it call itself.
It is not quite clear what you are trying to achieve with this call, so I can't give better advice, unfortunately.
Upvotes: 1