遇到 Array.each 遇到的错误,如:'syntax error, unexpected keyword_end, expecting end-of-input'

Having trouble with Array.each getting errors like: 'syntax error, unexpected keyword_end, expecting end-of-input'

我是 Ruby 的新手,因为我正在学习它作为 Stockfighter 计划的一部分 (https://www.stockfighter.io)。我正在尝试 link 跟上他们那里的 API,而且我过去没有遇到任何问题,所以我认为我的问题与我的 getAverage 有关函数,它应该采用一个值数组,将这些值加在一起,然后除以数组的长度,这样我就得到了这个模拟股票市场的每股平均价格。具体来说,我收到如下错误:

/Users/Kai/Dropbox/Kai/Stockfighter/level_three_selling.rb:41: syntax error, unexpected keyword_do_block, expecting keyword_end
        averageArr.each do |i|
                          ^
/Users/Kai/Dropbox/Kai/Stockfighter/level_three_selling.rb:51: warning: else without rescue is useless
/Users/Kai/Dropbox/Kai/Stockfighter/level_three_selling.rb:53: syntax error, unexpected keyword_end, expecting end-of-input

我查看了我的代码是否有错误,但找不到任何错误。我想我只需要第二双眼睛就能找到缺失的东西。也许我在某处遗漏了 end 或者我的语法错误。我以前从未遇到过这个问题,而且我已经在 Whosebug 和其他地方进行了一些研究,但是大多数其他人犯的与我类似的错误都会给出令人困惑的答案,这对像我这样的新手来说毫无意义。

require 'rubygems'
require 'httparty'
require 'json'

apikey = 'MY_KEY_IS_HERE'
venue = "VENUEX"
stock = "FOOBAR"

base_url = "https://api.stockfighter.io/ob/api"

account = "MY_ACCOUNT_IS_HERE"

averageArr = []

counter = 0


def getQuote(stock, venue, account)

    response = HTTParty.get("https://api.stockfighter.io/ob/api/venues/#{venue}/stocks/#{stock}/quote")

    orderbook = response.parsed_response

    puts orderbook["ok"]

    return orderbook["last"]
    puts orderbook["lastTrade"]

end


def getAverage(stock, venue, account)

    averageArr.push(getQuote(stock, venue, account))
    counter ++

    if counter >= 5

        sum = 0

        #FIRST ERROR POINTS TO HERE
        averageArr.each do |i|
            sum += i
        end

        return sum/5

    else

        return 'WAITING'

    #SECOND ERROR POINTS HERE
    end
#THIRD ERROR POINTS HERE
end

++ 不是 Ruby 运算符。你应该使用

counter += 1

getAverage方法的第二行。