Thread: Looping methods

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2015
    Posts
    2
    Rep Power
    0

    Question Looping 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:

    Code:
    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:

    Code:
    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!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2014
    Posts
    45
    Rep Power
    4
    retry can be used inside a rescue block, to jump back into the begin block after the condition that caused the exception has been remedied. Inside a block it has the effect of jumping to the beginning of the yielding method. So inside each this means that retry will jump to the beginning of the loop.

    return will return from the method it's inside of - in this case from other_start.

    break will return from the yielding method - in this case from each (which would be different from returning from a_method if something happened between the end of the each-block and the end of other_start).

    next will return from the block and thus jump to the next item in things.

    redo will jump to the beginning of the block and thus repeat the current iteration.

IMN logo majestic logo threadwatch logo seochat tools logo