Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - 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 
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 Classes and Objects
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    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 - Create a Class


    (Page 2 of 4 )

    Creating a class is simple; all you need to do is use the class statement, like so:


    class Soda

    end

    Classes follow a naming convention that insists the first letter must be capitalized. To add a method to a class, try the following:


    class Soda

    def initialize # initialize method is used to create objects

    @brand = "Mountain Dew" # store value in @brand

    end


    def get_brand # method to return the value of @brand

    return @brand

    end

    end

    What we did above is create a class named Soda. We then added a method to the class, initializing an instance variable (stores the data in an object and exists as long as the object does). You'll notice that our friend the instance variable starts with an @ symbol; all instance variables must start this way to tell the program that it is indeed an instance variable.

    You also stored data in the @brand variable and created a method to return the value of it (in this instance, "Mountain Dew").

    Create an Object

    To create an object in Ruby you use the new method.


    class Soda

    def initialize

    @brand = "Mountain Dew"

    end


    def retrieve_brand

    return @brand

    end

    end


    soda = Soda.new # creates the Soda object

    puts "The brand of the soda is " + soda.retrieve_brand

    You'll note that our soda object uses the naming convention of starting with a lowercase letter. Since the soda object has the retrieve_brand method built into it, you can call it like we did in the last line of code. Do you want to see what the program will print on your screen? Then run the program you lazy bum. Sheesh.

      The brand of the soda is Mountain Dew

    Ta da! You've created a class and an object.

    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-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT