Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - 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: 3 stars3 stars3 stars3 stars3 stars / 3
    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 - Constructors: More than Meets the Eye


    (Page 3 of 4 )

    In the above sample of code, the brand of soda always stays the same. But what if we want it to change? Well, we can use the class's constructor (the initialize method) to change it.

    (Note: the constructor creates new objects as well as manipulates the data within them).


    class Soda

    def initialize(brand)

    @brand = brand

    end


    def retrieve_brand

    return @brand

    end

    end


    soda = Soda.new("Pepsi")

    puts "The soda brand is now " + soda.retrieve_brand

    This will print the text: The soda brand is now Pepsi.

    Attributes

    In Ruby data items available outside of objects are known as Attributes. There are three types: Readable, Writeable, and Readable/Writeable.

    To create a Readable attribute do the following:


    class Soda

    attr_reader :brand


    def initialize(brand)

    @brand = brand

    end

    end


    soda = Soda.new("Dr. Salt")

    puts "You are now drinking " + soda.brand

    The result would be: You are now drinking Dr. Salt

    The statement attr_read :brand creates an accessor method by the name (brand) and creates an instance variable of the same name (@brand). And that is how you create a readable attribute.

    (Note: an accessor method allows you to access data inside an object).

    To create a Writeable attribute do the following:


    class Soda

    attr_reader :brand

    attr_writer :brand


    def initialize(brand)

    @brand = brand

    end

    end


    soda = Soda.new("Pepsi")

    puts "You are drinking " + soda.brand

    soda.brand = "Dr. Salt"

    puts "Sike...you are really drinking " + soda.brand

    This outputs:

      You are drinking Pepsi

      Sike...you are really drinking Dr. Salt

    Basically what happens is that you assign brand a value ("Pepsi"), read that brand and display it to the screen, then change the value using the writeable attribute to "Dr. Salt" and print that to the screen.

    As you can see, we not only made a writeable attribute, but a readable one as well. There is a simpler method for creating a readable/writeable attribute however:


    class Soda

    attr_accessor :brand


    def initialize(brand)

    @brand=brand

    end

    end


    soda = Soda.new("Pepsi")

    puts soda.brand + " Cola burnt him up"

    soda.brand = "7-UP."

    puts "Now he's drinking " + soda.brand

    This would create the following result:

      Pepsi Cola burnt him up

      Now he's drinking 7-UP.

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek