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


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 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials