help with while commands

J

Jorden Gershenson

hello,
I am very new to the ruby world and just to get some practice I
began writing a little program that allows you to interact (this all
through text mind you) with a baby dragon. Anyway, after i use gets for
"getsing" the name. I am having trouble stopping it from ending the
program as opposed to allowing me to type a command to interact with the
baby dragon. So far i have this:
class Dragon

boyg=rand(2)
if boyg==0
@gender='boy'
@gen='him'
end
if boyg==1
@gender='girl'
@gen='her'
end



puts 'Gongratulations, you are now the parent of a beautiful baby '
+ @gender + ' dragon!'
puts 'What will you name ' + @gen + '?'


@name=gets.chomp
puts 'Say hello to ' + @name + '!'


puts 'There are many things you can do with'
puts 'litte ' + @name + ' here!'
puts 'You can type commands such as...'
puts ''
puts 'feed'
puts 'sleep'
puts 'burp'
puts 'potty'
puts 'play'
puts 'Not to mention a few you are just going'
puts 'to have to find yourself!'
puts ''


def feed
puts '' + @name + ' licks ' + @gen + '\'s chops and jumps'
puts 'to catch the large hunk of meat.'
@stuffinBelly=10
passageofTime
end

def sleep
puts 'You put ' + name + ' to bed'
@asleep=true
3.times do
if @asleep
puts @name = ' snores, filling the room with smoke'
end

command= ''

while command != 'bye'
puts '' + @name + ' looks up at you...'
command=gets.chomp
end
end
end
end


Now i know i have very few interactions, but as i was maing them i
wanted to test them. When i realized i could not i whipped up this
little while and command thing you see at the bottom. When that didnt
work i came to the forums and further progress halted. Any help for a
begginer? Thanks in advance.

Jord1000
 
B

Bertram Scharpf

Hi,

Am Freitag, 10. Jul 2009, 11:25:58 +0900 schrieb Jorden Gershenson:
class Dragon

boyg=rand(2)
if boyg==0
@gender='boy'
@gen='him'
end
if boyg==1
@gender='girl'
@gen='her'
end

puts 'Gongratulations, you are now the parent of a beautiful baby ' @gender + ' dragon!'
puts 'What will you name ' + @gen + '?'

end

Just some ideas for fun:

class Dragon
GENDERS = { :boy => "him", :girl => "her" }
@gender = [ :boy, :girl][ (rand 2)]
@gen = GENDERS[ @gender]
puts <<-EOT
Gongratulations, you are now the parent of a beautiful baby #@gender dragon!
What will you name #@gen?
EOT
end

Nice what Ruby can turn to, isn't it?

Bertram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top