From: nick314 Date: 2007-08-19T18:24:02+09:00 Subject: Symbol as array index error ------=_NextPart_000_0034_01C7E285.38BEBAE0 Content-Type: text/plain; charset="big5" Content-Transfer-Encoding: quoted-printable 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=3D0, problems=3D0, hash=3D{}) @account =3D account @name =3D name=20 @penalty =3D penalty.to_i @problems =3D problems.to_i @hash =3D hash end =20 def add_problem begin_time, problem, result, submit_time if @hash.has_key?(problem) and @hash[problem] <=3D 0=20 return end # ... if result =3D=3D 2 @penalty +=3D submit_time - begin_time @problems +=3D 1 if @hash.has_key?(problem) @penalty +=3D @hash[problem] * 20*60 else=20 @hash[problem] =3D 0 end @hash[problem] *=3D -1 else if @hash.has_key?(problem) @hash[problem] +=3D 1 else @hash[problem] =3D 1 end end end def inspect "#@account, #@problems, #@penalty" end end # sort criteria =3D> high problems, low penalty, lexicographical account class Person include XmlSerialization=20 def <=3D>(other) if @problems =3D=3D other.problems if @penalty =3D=3D other.penalty return @account <=3D> other.account else return @penalty <=3D> other.penalty end else return other.problems <=3D> @problems end end end class ContestBoard include XmlSerialization attr_accessor :people, :begin_time, :problems def initialize begin_time, problems=3Dnil=20 @people =3D [] @begin_time =3D begin_time @problems =3D problems end=20 =20 def add account, problem, result, submit_time if find(account).nil? person =3D Person.new(account, User.find (account).name) person.add_problem(begin_time, problem, result, submit_time) @people << person else index =3D find(account) people[index].add_problem(begin_time, problem, result, = submit_time)=20 end end =20 def find account index =3D 0 @people.each { |p| if p.account =3D=3D account return index end index +=3D 1 } return nil end =20 def show for person in @people puts person.inspect end end =20 def sort @people =3D @people.sort end end class Serialization include XmlSerialization attr_accessor :classType, :type def initialize classType=3DContestBoard, type=3D"yaml" @classType =3D classType @type =3D type end =20 def serialize obj if @type =3D=3D "yaml"=20 return YAML_serialize obj elsif @type =3D=3D "xml" return XML_serialize(obj) end end def unserialize if @type =3D=3D "yaml" return YAML_unserialize obj=20 elsif @type =3D=3D "yaml" return XML_unserialize obj end end =20 private def YAML_serialize obj return YAML.dump(obj)=20 end =20 def YAML_unserialize sObj return YAML.load(sObj) end =20 def XML_serialize obj return obj.to_xml end =20 def XML_unserialize sObj doc =3D REXML::Document.new(sObj); return @classType.from_xml(doc.root ) end end # ... class ContestsController < ApplicationController def index list render :action =3D> 'list' end # GETs should be safe (see = http://www.w3.org/2001/tag/doc/whenToUseGet.html) verify :method =3D> :post, :only =3D> [ :destroy, :create, :update ], :redirect_to =3D> { :action =3D> :list } # contest_board method def contest_board=20 @now_contest =3D Contest.find( get_now_contest ); if !@now_contest.nil? solveinfos =3D @now_contest.solveinfos.find(:all, :order =3D> = "created_at ASC"); #solveinfos =3D @now_contest.solveinfos.find(:all, :conditions = =3D> "solveStatus =3D 2", :order =3D> "created_at ASC");=20 # get many solveinfo @board =3D ContestBoard.new(@now_contest.from) for solveinfo in solveinfos @board.add(solveinfo.userID, solveinfo.problemID, = solveinfo.solveStatus, solveinfo.created_at ); end @board.sort @users =3D User.find(:all) =20 #@past_contest =3D PastContests.new #@serialization =3D Serialization.new(@board) #@past_contest.contest_id =3D @now_contest.id=20 #@past_contest.format =3D 'yaml' #@past_contest.serialization =3D @serialization.serialize #@past_contest.save =20 puts @board.to_xml #@past_contest =3D PastContests.new=20 #@serialization =3D Serialization.new(ContestBoard, 'xml') #@past_contest.contest_id =3D @now_contest.id #@past_contest.format =3D 'xml' #@past_contest.serialization =3D @serialization.serialize @board #@past_contest.save =20 end end =20 def contest_board_test @board =3D ContestBoard.new @board.add('nick314', 1, 1, 10) @board.add('nick314', 1, 2, 10)=20 @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=20 def get_now_contest=20 # there won't be two contests overlayped now =3D Time.now for contest in Contest.find(:all) if now >=3D contest.from and now < contest.to return contest.id end end return nil end =20 # list method=20 def list @contest1 =3D Contest.find(1) @solveinfo =3D @contest1.solveinfos @solved_problem =3D @ solveinfo.find(:all) # this is wrong #@contest1.solveinfos << Solveinfo.find( 221 ) @contests_pages, @contests =3D paginate :contests, :per_page =3D> 10 end # show method=20 def show=20 @contests =3D Contest.find(params[:id]) end # new method=20 def new @contests =3D Contest.new end =20 # create method=20 def create @contests =3D Contest.new(params[:contests]) if @contests.save flash[:notice] =3D 'Contests was successfully created.' redirect_to :action =3D> 'list' else render :action =3D> 'new' end end # edit method=20 def edit @contests =3D Contest.find(params[:id]) end # update method=20 def update @contests =3D Contest.find(params[:id]) if @contests.update_attributes(params[:contests])=20 flash[:notice] =3D 'Contest was successfully updated.' redirect_to :action =3D> 'show', :id =3D> @contests else render :action =3D> 'edit' end end # destroy method=20 def destroy Contest.find(params[:id]).destroy redirect_to :action =3D> 'list' end end --=20 I am Nick314=20 ------=_NextPart_000_0034_01C7E285.38BEBAE0--