Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - 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 - Inheritance


    (Page 4 of 4 )

    Yes, it's what we're all waiting for: that big fat pay check from our dear old dead uncle whom we hardly knew. I mean, it was great catching the baseball with him and all, but now that he left his gigantic fortune, you can buy your own baseball team and have them throw you the ball. Or better yet, you can buy a whole squad of cheerleaders and have them...well cheer.

    While not nearly as exciting as our above example, Ruby (like other programming languages) allows you to pass the things one class has down to another.

    Let's say we start a new class, called oh I don't know...computer programmers. Maybe we'll call it Nerds for short. What are some things you might notice about a nerd? They might wear funny glasses and pocket protectors. When they go to the salon they might have the hair stylist place a bowl on their head and trim around it. Maybe they wear those brown corduroys pulled up to their neck so that they look like a turtle. These are all things we can say a nerd (or at least the cliche of a nerd) possesses.

    But maybe the nerd class is too broad. What if we want a small group of people. How about Ruby Nerds? Now although Ruby Nerds is a new class, let's face it...in the grand scheme of things they are still Nerds, and thus possess the same things. This my friends, is inheritance.

    Basically inheritance it saves the programmer time and coding; he no longer needs to describe the new class, since it is a subclass of a class and possesses the same traits since it is from the same background.

    Here's how that looks in code:


    class Nerd # Superclass

    def initialize(action)

    @action = action

    end


    def retrieve_action

    return @action

    end

    end


    class Ruby_Nerd < Nerd # informs program Ruby_Nerd is a child of Nerd

    def initialize (action, reaction)

    super(action) #says to get the action from the superclass

    @reaction = reaction

    end


    def retrieve_reaction

    return @reaction

    end

    end


    ruby_nerd = Ruby_Nerd.new("trip", "red")

    puts "You " + ruby_nerd.retrieve_action + "in front of a girl!"

    puts "Your face turns " + ruby_nerd.retrieve_reaction + ruby_nerd.retrieve_reaction + ruby_nerd.retrieve_reaction

    The previous code would produce the following output:

      You trip in front of a girl!

      Your face turns red red red

    Well, we covered a lot of ground in this tutorial, but surprisingly enough, there is still more you need to know about Objects and Classes. We'll have to get to that in the next tutorial however, because I am out of space here. So join us next time, when we'll learn more about the awesome things you can do with Ruby.


    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.

       · 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 3 hosted by Hostway
    Stay green...Green IT