Home arrow Ruby-on-Rails arrow Page 5 - 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 - Scope
(Page 5 of 5 )

You can define variables within methods (giving them local scope). This means that you can only use the variable within the method and not outside of it.

secret_agent = “James Bond”

def spy_man()

text = “Bond”

puts text



puts text

This code will print the following:


  James Bond

As you can see in the code we have two variables named text. Normally this is not allowed. But since one of the variables named text is within a method, and therefore has local scope, the program does not see it outside of the method and allows you to have two variables with the same name.

That concludes this episode of Ruby for the Newbie. In the next article we will look at Classes and Objects, and beyond that we will explore working with Rail and creating web pages. So check back often for more articles on these topics.

Till then...

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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