From: Jon Garvin Date: 2008-04-23T03:52:48+09:00 Subject: Re: Where did the monsters go? Ben Galyean wrote: > Stuck again. My random monster-fight generator works, but it's so BASIC. > How can I do this without the if-statements? Is it a bad thing to stick > class objects into an array? > > Also, I know Moblist[0].fight works, but why not Moblist[0..2].fight ? > > > ############################################## > class Mob > def fight > "THIS IS A FIGHT METHOD" > end > end > > GOBO = Mob.new > DRAGON = Mob.new > SLIME= Mob.new > > Moblist = [GOBO, DRAGON, SLIME] > > def rand_fight > a = rand(Moblist.length)#<-----------Has to be a better way| > if a ==0 then GOBO.fight > elsif a==1 then DRAGON.fight > elsif a==2 then SLIME.fight > end#------------------------------------------------------| > > end > > p rand_fight > ############################################## > Try this.... def rand_fight monster = Moblist[rand(Moblist.length)] monster.fight end or... def rand_fight Moblist[rand(Moblist.length)].fight end -- http://www.5valleys.com/ http://www.workingwithrails.com/person/8078