Home arrow Ruby-on-Rails arrow Page 4 - Ruby Classes and Objects
RUBY-ON-RAILS

Ruby Classes and Objects


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.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
October 23, 2007
TABLE OF CONTENTS:
  1. · Ruby Classes and Objects
  2. · Create a Class
  3. · Constructors: More than Meets the Eye
  4. · Inheritance

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials