Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Ruby Loops
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Ruby Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-10-22

    Table of Contents:
  • Ruby Loops
  • The While Loop
  • Stop that Loop!
  • Reach Out and Touch Someone
  • Scope

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Ruby Loops - Reach Out and Touch Someone


    (Page 4 of 5 )

    Remember when that used to be AT&T's slogan? Probably not the brightest advice. Not to mention that if you could reach out and touch them, why would you call them? I mean they would literally be within an arm's reach of you.

    Anyway, in this section we are going to learn how to create our own methods and call upon them to do our bidding. Muahhaha!


    def own_you

    puts “I PWN YOU!”

    end

    In the above example, we created a method named own_you. If we were to call it (and we will), it would print the sentence: I PWN YOU! to your computer screen. Let's go ahead and call it. No, not like that; put away your cell phone.


    def own_you

    puts “I PWN YOU!”

    end


    own_you

    And there you have it; simple as can be. Note that the method will not run until you call it. Unlike my plethora of dogs, who won't come even when you do call them.

    Arguing with Methods

    Are methods really getting on your nerves? Go ahead, argue with them. Feel better? Well you shouldn't. All those methods ever did is whatever you asked.

    In Ruby you can pass arguments to methods quite easily:


    def insult(mama_joke)

    puts mama_joke

    end


    insult “Yo mama so fat she wore an X-Men jacket and helicopters landed on her back”

    This would result in the following print out:

      “Yo mama so fat she wore an X-Men jacket and helicopters landed on her back”

    You could also use the following method:


    def bad_movie(first_name, second_name, third_name)

    puts first_name + “ “ + second_name + “ “ + third_name

    end


    bad_movie “I”,“Heart”,”Huckabees.”

    This would print out: I Heart Huckabees.

    The “ “ in the puts line tells the program to add a space, while the + symbol concatenates the strings together.

    More Ruby-on-Rails Articles
    More By James Payne


       · The series on Ruby continues! In this episode we discuss loops, including the While,...
     

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT