Reputation: 3080
I get the following error when I try sorting a array with objects inside
undefined method `match_id' for #
I am getting the object back fine without calling sort on it (both sort attempts result in same error)
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
The object is created in the following function (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
What is it with the sort that I am doing wrong?
Upvotes: 0
Views: 77
Reputation: 160211
You're assuming it's an object with a match_id
method, whereas it appears to be a simple hash.
a[:match_id] <=> b[:match_id]
Upvotes: 1