I don’t know why this code got Symbol as array index error.
Please help me out.
nick314
cl/xmlserial is the library of REXML
require ‘yaml’
require ‘cl/xmlserial’
class Person
include XmlSerialization
attr_accessor :account, :name, :penalty, :problems, :hash
def initialize(account, name, penalty=0, problems=0, hash={})
@account = account
@name = name
@penalty = penalty.to_i
@problems = problems.to_i
@hash = hash
end
def add_problem begin_time, problem, result, submit_time
if @hash.has_key?(problem) and @hash[problem] <= 0
return
end
# …
if result == 2
@penalty += submit_time - begin_time
@problems += 1
if @hash.has_key?(problem)
@penalty += @hash[problem] * 20*60
else
@hash[problem] = 0
end
@hash[problem] *= -1
else
if @hash.has_key?(problem)
@hash[problem] += 1
else
@hash[problem] = 1
end
end
end
def inspect
“#@account, #@problems, #@penalty”
end
end
sort criteria => high problems, low penalty, lexicographical account
class Person
include XmlSerialization
def <=>(other)
if @problems == other.problems
if @penalty == other.penalty
return @account <=> other.account
else
return @penalty <=> other.penalty
end
else
return other.problems <=> @problems
end
end
end
class ContestBoard
include XmlSerialization
attr_accessor :people, :begin_time, :problems
def initialize begin_time, problems=nil
@people = []
@begin_time = begin_time
@problems = problems
end
def add account, problem, result, submit_time
if find(account).nil?
person = Person.new(account, User.find (account).name)
person.add_problem(begin_time, problem, result, submit_time)
@people << person
else
index = find(account)
people[index].add_problem(begin_time, problem, result,
submit_time)
end
end
def find account
index = 0
@people.each { |p|
if p.account == account
return index
end
index += 1
}
return nil
end
def show
for person in @people
puts person.inspect
end
end
def sort
@people = @people.sort
end
end
class Serialization
include XmlSerialization
attr_accessor :classType, :type
def initialize classType=ContestBoard, type=“yaml”
@classType = classType
@type = type
end
def serialize obj
if @type == “yaml”
return YAML_serialize obj
elsif @type == “xml”
return XML_serialize(obj)
end
end
def unserialize
if @type == “yaml”
return YAML_unserialize obj
elsif @type == “yaml”
return XML_unserialize obj
end
end
private
def YAML_serialize obj
return YAML.dump(obj)
end
def YAML_unserialize sObj
return YAML.load(sObj)
end
def XML_serialize obj
return obj.to_xml
end
def XML_unserialize sObj
doc = REXML::Document.new(sObj);
return @classType.from_xml(doc.root )
end
end
…
class ContestsController < ApplicationController
def index
list
render :action => ‘list’
end
GETs should be safe (see
http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
contest_board method
def contest_board
@now_contest = Contest.find( get_now_contest );
if !@now_contest.nil?
solveinfos = @now_contest.solveinfos.find(:all, :order =>
“created_at ASC”);
#solveinfos = @now_contest.solveinfos.find(:all, :conditions =>
“solveStatus = 2”, :order => “created_at ASC”);
# get many solveinfo
@board = ContestBoard.new(@now_contest.from)
for solveinfo in solveinfos
@board.add(solveinfo.userID, solveinfo.problemID,
solveinfo.solveStatus, solveinfo.created_at );
end
@board.sort
@users = User.find(:all)
#@past_contest = PastContests.new
#@serialization = Serialization.new(@board)
#@past_contest.contest_id = @now_contest.id
#@past_contest.format = 'yaml'
#@past_contest.serialization = @serialization.serialize
#@past_contest.save
puts @board.to_xml
#@past_contest = PastContests.new
#@serialization = Serialization.new(ContestBoard, 'xml')
#@past_contest.contest_id = @now_contest.id
#@past_contest.format = 'xml'
#@past_contest.serialization = @serialization.serialize @board
#@past_contest.save
end
end
def contest_board_test
@board = ContestBoard.new
@board.add(‘nick314’, 1, 1, 10)
@board.add(‘nick314’, 1, 2, 10)
@board.add(‘nick314’, 2, 1, 10)
@board.add(‘nick314’, 2, 1, 10)
@board.add(‘nick314’, 2, 2, 10)
@board.sort
end
get_now_contest method
def get_now_contest
# there won’t be two contests overlayped
now = Time.now
for contest in Contest.find(:all)
if now >= contest.from and now < contest.to
return contest.id
end
end
return nil
end
list method
def list
@contest1 = Contest.find(1)
@solveinfo = @contest1.solveinfos
@solved_problem = @ solveinfo.find(:all)
# this is wrong
#@contest1.solveinfos << Solveinfo.find( 221 )
@contests_pages, @contests = paginate :contests, :per_page => 10
end
show method
def show
@contests = Contest.find(params[:id])
end
new method
def new
@contests = Contest.new
end
create method
def create
@contests = Contest.new(params[:contests])
if @contests.save
flash[:notice] = ‘Contests was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
edit method
def edit
@contests = Contest.find(params[:id])
end
update method
def update
@contests = Contest.find(params[:id])
if @contests.update_attributes(params[:contests])
flash[:notice] = ‘Contest was successfully updated.’
redirect_to :action => ‘show’, :id => @contests
else
render :action => ‘edit’
end
end
destroy method
def destroy
Contest.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end