Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Ruby Classes and Objects
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 
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 Classes and Objects
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-10-23

    Table of Contents:
  • Ruby Classes and Objects
  • Create a Class
  • Constructors: More than Meets the Eye
  • Inheritance

  • 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 Classes and Objects


    (Page 1 of 4 )

    In the previous articles, we got into some of the simpler aspects of Ruby. In this tutorial, we are going to cover the area of Classes and Objects. While still pretty basic in concept, Classes and Objects are powerful tools that will assist in your efforts to take over the world -- or at least to master all of the wonderful things that Ruby can do.

    So put aside those 20-sided dice, pack up your Magic: the Gathering cards, and close that game of Civilization you have running on your computer. It's time to do something productive with that silicon box. Roll up your sleeves and let's get to work.

    Class: You Have it or You Don't

    It's true. Take Ben Franklin for example. He was classy enough to be a respected writer, inventor, and an ambassador to France for a nation about to be born -- plus hardcore enough to fly kites in thunderstorms. (He must have also been a ninja; I mean even Chuck Norris won't do that!). That's class. Unfortunately, Ben may have been a wise old man, but he didn't know anything about coding in Ruby, so this won't really help you. 

    An easy way to think of classes in Ruby is to consider the beverage industry (no, it isn't time for a beer yet). There are a wide variety of beverages to choose from. Soda, water, milk, juice, whatever Yoohoo is (delicious I think is the only real way to describe it), energy drinks, and Kool-Aid -- the poor man's champagne. Each of those (well not Yoo-hoo and Kool-Aid) would be considered a group of beverages, or a class. For instance, Soda is a class of beverages. Now within that class you can have a variety of unique drinks: Coca-Cola, Pepsi, Sprite, Dr. Pepper, Jolt, Mountain Dew (the nectar of the gods), etc.

    Whether you know it or not, I just not only described Classes (the beverage type) but also Objects (the individual sodas that make up the Class). Put simply, the class acts as the type of an object. Class: Soda. Object: Mountain Dew.

    The last thing you need to know is this: You use classes to create objects.

    More Ruby-on-Rails Articles
    More By James Payne


       · This article covers Classes and Objects in Ruby, as well as the use of Constructors...
     

    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-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT