iterating through a block; declaring an index/counter within that block

Discussion in 'Ruby' started by Thufir, Oct 20, 2007.

  1. Thufir

    Thufir Guest

    Where does "food" come from in the example below? The declaration is
    within the block? The varaible food exists only within the block?
    What is (are) the value(s) for "food"?




    Read the following aloud to yourself.

    ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }

    While this bit of code is less readable and sentence-like than the
    previous examples, I'd still encourage you to read it aloud. While
    Ruby may sometimes read like English, it sometimes reads as a shorter
    English. Fully translated into English, you might read the above as:
    With the words 'toast', 'cheese', and 'wine': take each food and print
    it capitalized.

    The computer then courteously responds: Toast, Cheese and Wine.

    <http://poignantguide.net/ruby/chapter-3.html#section1>





    thanks,

    Thufir
     
    Thufir, Oct 20, 2007
    #1
    1. Advertising

  2. Thufir

    7stud -- Guest

    Thufir wrote:
    > Where does "food" come from in the example below? The declaration is
    > within the block? The varaible food exists only within the block?
    > What is (are) the value(s) for "food"?
    >
    >
    >
    >
    > Read the following aloud to yourself.
    >
    > ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
    >


    That thing on the right of each, between the braces, is a similar to a
    method definition:


    def func(food)
    print food.capitalize
    end

    food is the parameter variable and you would call func like this:

    func('toast')

    'toast' then gets assigned to the parameter variable food.

    each() is also method. each() is defined to pass each member of the
    thing to its left to the block on its right. It is similar to doing
    this:

    def each(arr)
    count = 0

    while count < arr.length
    curr_elmt = arr[count]
    my_func(curr_elmt)
    count += 1
    end

    end


    def my_func(food)
    print food.capitalize
    end

    arr = ['toast', 'cheese', 'wine']
    each(arr)

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Oct 20, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Rodríguez

    getting the index while iterating through a list

    Fernando Rodríguez, May 12, 2004, in forum: Python
    Replies:
    4
    Views:
    450
    Steven Rumbalski
    May 12, 2004
  2. carl
    Replies:
    5
    Views:
    2,458
    James Kanze
    Nov 25, 2009
  3. Ge Bro
    Replies:
    16
    Views:
    194
    Giles Bowkett
    Nov 26, 2007
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    337
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    207
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page