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

Ruby Classes and Objects
(Page 1 of 4 )

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.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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