简体   繁体   English

不断接收-SystemStackError:堆栈级别太深

[英]keep receiving - SystemStackError: stack level too deep

My Code Runs When I Test It Within The Program, But When I Try To run Rspec on The Code It Throws a Error 当我在程序中对其进行测试时,我的代码将运行,但是当我尝试在该代码上运行Rspec时,它将引发错误

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
 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. let工作方式是从本质上定义一个方法,该方法的名称是您传递给letSymbol ,主体是块。 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. 如您所见, array调用自身,并且没有任何条件可以停止这样做,因此您可以获得无穷的递归。 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. 目前尚不清楚您要通过此电话实现的目标,因此很遗憾,我无法提供更好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM