Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialThomas Perkins
496 Pointslooping methods
Hello, I am new to using Ruby so I apologize if my question seems fairly easy.
I am trying to loop a method back the the beginning of the method for example:
def start
code
end
gets.chomp
def other_start
puts "please type yes"
choice = gets.chomp!
end
end
start
I want to loop the method "other_start" to the method "start" if the user types "yes"
Now I know how to do loops but I don't know how I would loop a method to another method, would it be the same thing as just looping if the input equals the answer I'm looking for? Such as:
other_start.each do |start|
If you could help me out with the syntax and explain what it's doing, that would be greatly appreciated. Thank you ahead of time!
7 Answers
Steve Hunter
57,712 PointsOK - so if the user responds with "yes" you want to call the start
method, yeah?
You've got the if
statement in there, although I have no idea what =~ /yes/i
means, I have to say!
So, just after that is the code that will run if that condition is met - can't you just call the start
method from there, before the elsif
?
Something along the lines of:
choice = gets.chomp
if choice == "Yes"
start # call the start method
else
# choice was probably 'no'!
# exit program
end
I've not got a Ruby environment to hand so can't try out any ideas!
steve.
Steve Hunter
57,712 PointsIgnore the code for now - what is it you are trying to create here? Define the outline of what should happen and we can work out the best way to write that in code.
Steve.
Thomas Perkins
496 PointsI'll just send the whole thing. I'm learning right now off of learnrubythehardway and one of the exercises are to make a game here's what I have so far..
def start
puts "You walk home at night and realize there's a creepy abandoned house across the street from you. For some reason you think it's an excellent idea to walk into the creepy ass house."
puts "So you walk in and realize that there's a long creepy hallway and you immediatly regret your decision, but you turn around and, THE DOOR IS GONE!!"
puts "You panic and start running down the hallway, you come across two doors, one on the left, and one on the right. 'Great' you think to yourself. These doors are the only way out from what you have gathered. Which door do you choose?"
print "> "
choice = gets.chomp!
if choice =~ /left/i
corpse_room
elsif choice =~ /right/i
water_room
else
dead("You choose not to go into a door, you stumble around till you starve.")
end
end
def freedom
puts "You have survived through the tasks that have been presented to you. would you like to play again?"
choice = gets.chomp!
if choice =~ /yes/i
elsif choice =~ /no/i
puts "Thank you for playing, hope you enjoyed!"
else
puts "You may exit the screen at anytime."
end
end
def corpse_room
puts "You walk into a dark room and there's an over bearing smell of death. You look around as your eyes adjust to the dark and see dead bodies everywhere, one of the dead bodies is hanging by a hook in the middle of the room. You look at it and notice there's a chain around it's neck."
Steve Hunter
57,712 PointsWhich exercise of that course is this? I'll look it up.
Which bit of code do you want to complete?
Thomas Perkins
496 PointsIdk why it did the two boxes..?
Thomas Perkins
496 Pointsexercise 36.
I'm trying to loop
def freedom puts "You have survived through the tasks that have been presented to you. would you like to play again?" choice = gets.chomp!
if choice =~ /yes/i
to the beginning of the game if the answer is "yes"
more specifically I want the game to start over from
def start puts "You walk home at night and realize there's a creepy abandoned house across the street from you. For some reason you think it's an excellent idea to walk into the creepy ass house." puts "So you walk in and realize that there's a long creepy hallway and you immediatly regret your decision, but you turn around and, THE DOOR IS GONE!!" puts "You panic and start running down the hallway, you come across two doors, one on the left, and one on the right. 'Great' you think to yourself. These doors are the only way out from what you have gathered. Which door do you choose?"
If they answer yes to the 'freedom' question
Thomas Perkins
496 Points=~ /yes/i just makes it so that no matter how they spell 'yes' it will accept it. Such as Yes YES yEs yES yeS etc.
So if i just called the start method under there is would work? I wouldn't have to loop it? I feel kinda dumb now lol..
Steve Hunter
57,712 PointsA loop repeats something. So a while
loop repeats while something remains true - while it is sunny; wear shades
. A for
loop iterates through lists/arrays for each name in class - ask if they are present
.
You don't need to repeat anything - you just need to move the execution position. So, just call the method, I think!
Thomas Perkins
496 PointsWell thank you! It worked and you are my savior
Steve Hunter
57,712 PointsAwesome! Glad to help. :-)