SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I don't understand Ruby "pipes" for argument

    here is an example code from a book:

    Code:
    ['toast', 'cheese', 'wine']. each { |food| print( food.capitalize ) }
    notice the |food| in there?, I don't understand that part.

    Since I can understand php a little, what would the above code look like in php?

  2. #2
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Site Owner View Post
    Since I can understand php a little, what would the above code look like in php?
    |food| is the thing that refers back to each item in the array. In PHP you would do this:
    Code:
    $foods = array('toast', 'cheese', 'wine');
    foreach($foods as $food) {
      echo strtoupper($food);
    }
    More here: http://www.rubycentral.com/ref/ref_c_array.html#each

    HTH!

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I think I get it, not too sure...not what is does but the way it's laid out. Leaves me wondering, couldn't they have made it more obvious?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's another language concept. Hard to get at first but really good when you understand it. PHP has no equivalent to blocks.

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This section of the pickaxe is worth looking through as it goes over working with blocks.

    If you find it easier, you can do this in Ruby:
    Code:
    foods = ['toast', 'cheese', 'wine']
    for food in foods
      print food
    end


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •