0

当我尝试对包含对象的数组进行排序时出现以下错误

# 未定义的方法“match_id”

我没有调用 sort 就可以很好地恢复对象(两次排序尝试都会导致相同的错误)

get '/' do
  content_type :json

  @matches = []

  build_matches_object(@matches, 'C:\Users\Steve\Desktop\BoxRec Boxing Records_files\BoxRec Boxing Records.htm')


  @matches.sort! { |a,b| a.match_id <=> b.match_id }
  #@matches.sort_by { |a| [a.match_id] }

  @matches.to_json
end

该对象在以下函数中创建(build_matches_object)

def build_matches_object(myscrape, boxrec_path)
  doc = Nokogiri::HTML(open(boxrec_path))

  match_date = ''

  doc.xpath("//table[@align='center'][not(@id) and not(@class)]/tr").each do |trow|

    #Try get the date
    if trow.css('.show_left b').length == 1
      match_date = trow.css('.show_left b').first.content
      match_date = Time.parse(match_date)
    end

    #if a match row
    if trow.css('td a').length == 2 and trow.css('* > td').length > 10

      #CODE REMOVED THAT GETS THE BELOW VARIABLES USED TO BUILD MATCH (KNOW IT RETURNS THEM FINE

      #create the match object
      match = {
        :number_of_rounds => trow.css('td:nth-child(3)').first.content.to_i,
        :weight_division => trow.css('td:nth-child(4)').first.content,
        :first_boxer_name => first_boxer_td.css('a').first.content,
        :first_boxer_href => first_boxer_href,
        :second_boxer_name => second_boxer_td.css('a').first.content,
        :second_boxer_href => second_boxer_href,
        :date_of_match => match_date,
        :rating => rating,
        :match_id => matchid
      }  

      myscrape.push(match)

    end
  end

end

我做错了什么?

4

1 回答 1

1

您假设它是一个带有match_id方法的对象,而它似乎是一个简单的散列。

a[:match_id] <=> b[:match_id]
于 2012-05-30T20:54:17.910 回答