The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Hal's if..each

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Hal's if..each Posted: Apr 21, 2005 11:59 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Hal's if..each
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

I’m contemplating putting a difficulty level on posts in the “bits” and “inspect” sections. A simple rating of 1 to 5. I’d consider the following code to be an even 3. These kinds of examples are the sweet spot for moving past beginner.

It’s at this stage that you’re familiar enough to chain Ruby constructs together creatively. Every Ruby construct returns a value of some kind. How can that return be used?

Hal Fulton’s if..each uses each to loop through a list unless the list is empty. In which case the second bit of code (which prints "no songs!") is executed.

 if song_list.each do |song|
   print song.name
 end.empty?
   print "no songs!" 
 end

The fragment is made tricky because the indentation fools you. Sky punchers have the advantage here. (found in for..in..else? Please note that Hal’s was originally an unless..each but testing revealed that if..each is the correct.)

Read: Hal's if..each

Topic: Attempting to work with OSX..much to learn Previous Topic   Next Topic Topic: Rails extends warm welcome to PHP developer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use