# Where did the monsters go?

**URL:** <https://www.ruby-forum.com/t/where-did-the-monsters-go/135522>\
**Category:** Ruby\
**Created:** [April 22, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/where-did-the-monsters-go/135522 "2008-04-22T20:36:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [April 22, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/where-did-the-monsters-go/135522/1 "2008-04-22T20:36:42Z")

</div>

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  
##############################################

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [April 22, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/where-did-the-monsters-go/135522/2 "2008-04-22T20:45:14Z")

</div>

Ben G. 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?  
> Yep.

def rand\_fight  
Moblist[rand(Moblist.size)].fight  
end

> Also, I know Moblist[0].fight works, but why not Moblist[0…2].fight ?  
> Because array[range] returns an array.

And btw…

> Moblist = [GOBO, DRAGON, SLIME]  
> Why are you using constants for all this?

You can also initialize your list something like  
moblist = []  
3.times do  
moblist \<\< Mob.new  
end

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [April 22, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/where-did-the-monsters-go/135522/3 "2008-04-22T20:52:18Z")

</div>

Ben G. 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  
> ##############################################

As the great Julian R. once said, one level of indirection solves  
everything. 🙂

> Also, I know Moblist[0].fight works, but why  
> not Moblist[0…2].fight ?

Because [0…2] is a Range object, which does not respond to the fight  
method. You could monkey patch (_ducks_) Range to make it work, but a  
better idea would be to take a step back and create a class responsible  
for representing multiple monsters. GroupOfMonsters or MonsterParty,  
perhaps? 😉

As with your rand(Moblist.length) bit, that should also be put into its  
own class (or several classes). Think about what you’re really trying to  
say, then figure out the players (objects) and who is responsible for  
doing what.

If you’re trying to write anything remotely resembling a game, you’ve a  
lot of work ahead of you.

Have you looked at \_why’s creature code?  
[http://poignantguide.net/ruby/chapter-6.html](http://poignantguide.net/ruby/chapter-6.html)

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [April 22, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/where-did-the-monsters-go/135522/4 "2008-04-22T20:55:14Z")

</div>

Ben G. wrote:

> ```
> "THIS IS A FIGHT METHOD"
> 
> ```
> 
> 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.5valleys.com/)

[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [April 22, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/where-did-the-monsters-go/135522/5 "2008-04-22T21:01:42Z")

</div>

On Tue, Apr 22, 2008 at 1:36 PM, Ben G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> "THIS IS A FIGHT METHOD"
> 
> ```
> 
> 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  
> ##############################################

I’m not sure why you use constants, but…

class C  
def fight  
puts ‘hi, nice to meet you’  
puts 'I am " \<\< self. **id**.to\_s  
end  
end

a = Array.new(3) {C.new}  
puts a[rand(a.size)].fight

Just to illustrate.

Todd

---

<div class="post-metadata">

**Author:** ![gastrogreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gastrogreen](https://www.ruby-forum.com/u/gastrogreen)\
**Post date:** [April 22, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/where-did-the-monsters-go/135522/6 "2008-04-22T21:48:22Z")

</div>

On Tue, Apr 22, 2008 at 2:00 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > class Mob
> > 
> > ##############################################
> 
> I’m not sure why you use constants, but…
> 
> class C  
> def fight  
> puts ‘hi, nice to meet you’  
> puts 'I am " \<\< self. **id**.to\_s

Once again that weird todd person makes a slight mishap 🙂 That double  
quote is supposed to be a single one. And I really wanted to say  
something a little more sinister, like “So, we meet again }\>),  
muahaha.” but the nice thing is kind of funny.

Todd
