1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Loops & Iterators in Ruby

Discussion in 'Ruby on Rails' started by pradeep, Sep 15, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:

    Loops



    Ruby provides the basic while and unless loops and iterators. Let us go through while and until first before looking at iterators.

    A "while" loop defines that a statement or block of code is to be rerun time and time again while a condition remains true, and then execution is to continue after the statement or block. An "until" loop says that the block is to be performed until the condition goes true (i.e. while it is false).

    Just like the "if" and "unless", "while" and "until" are written with an 'end' key-worded block, or as modified after a piece of code.

    Code:
     top = 4
     
     # A while loop, counting up
     now = 0
     while now < top
         puts "while #{now} .. #{top}"
         now += 1
         end
     
     
     # While written in one line
     now = 0
     print "#{now+=1} " while now < top
     print "\n"
     
     
     # An until loop
     now = 0
     until now > top
         puts "until #{now} .. #{top}"
         now += 1
         end
     
     
     # Until written in one line
     total = 1
     bugs = 19
     needed *= 2 until needed >= bugs
     print "You have #{teams} bugs so ideally ",
             "there are #{total}.\n"
     
    Output:
    Code:
     while 0 .. 4
     while 1 .. 4
     while 2 .. 4
     while 3 .. 4
     1 2 3 4
     until 0 .. 4
     until 1 .. 4
     until 2 .. 4
     until 3 .. 4
     until 4 .. 4
     You have 19 teams so ideally need 32.
     

    Iterators



    The method "each" when run on what we call a collection object will cause a block of code to be run on each of the elements of the collection object in turn. Yes, that's rather different to many other languages (though it does parallel foreach in PHP and for in Python).

    You can write a loop that looks reasonably conventional to step up through a series of numbers - example coming up - but in addition you can write far clearer and more elegant loops to process all the members of a collection in turn, without having to refer to them by their position number all the time.

    Code:
     # Some for loops that might almost look familiar
     
     # From 1 to 20
     for k in 1..20
        print k," "
     end
     print "\n"
     
     
     # From 1, stopping short of 20
     for k in 1...20
        print k," "
     end
     print "\n"
     
     
     # An iterator object
     (5..15).each do |k|
        print k," "
     end
     print "\n"
     
    Output:
    Code:
     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
     5 6 7 8 9 10 11 12 13 14 15
     
    Note: The .. and ... operators, and the subtle difference - in the first case, returns an iterator that runs up from the first to the last number, but in the second case the iterator stops one short of the last number.
     

Share This Page