Home arrow Ruby-on-Rails arrow Page 4 - Ruby Loops

Ruby Loops

After last week, I know you've been eagerly waiting for this, the fifth article in my Ruby on Rails series. This one will give you an in-depth look at Loops, Methods, and Blocks. Well maybe Blocks. You'll have to see. So without a bunch of unnecessary yammering, let's get to it.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 16
October 22, 2007
  1. · Ruby Loops
  2. · The While Loop
  3. · Stop that Loop!
  4. · Reach Out and Touch Someone
  5. · Scope

print this article

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!”


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!”



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


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


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.

Next: Scope >>

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials