Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Ruby (http://www.programmingforums.org/forum63.html)
-   -   Ruby Classes tutorial (http://www.programmingforums.org/showthread.php?t=5371)

bulio Aug 11th, 2005 2:39 PM

Ruby Classes tutorial
 
I created a tutorial to help me while learning Ruby. I've decided to post it for all to see :) Please let me know of any syntax errors if you see them, so I may fix them.

Classes

Library example:

Ok, so I want to start a library. In the library, I need books. We need to make a basic class, books. This is where all the books will go.

We'll start off by creating a basic class for all the books.
Quote:

class Books
def initialize(title,year,author)
@title = title
@year = year
@author = author
end
end
In there, I just made a general class, books. Every book in the library will have a title, year, and an author. The class tells the computer that information.

Now lets enter a book into the database:
Quote:

book1 = Book.new("Ruby Guide","2005","bulio")
book1.inspect
Now we just added book1 to our library databse! book1.inspect shows us some information about the file.

Now we want to try and get all the book information. So we will do this:
Quote:

class Books
def to_s
"Books: #{@title}--#{@year} (#{@author})"
end
end
book1 = Books.new("Ruby Guide", 2005," bulio")
book1.to_s
That will output:
Quote:

book1.to_s » "Books: Ruby Guide--2005 (bulio)"
Now thats some headway!

Inheritance and Messages

Inheritance allows you to create a class that is a specialization of another class. (Think of it as a subclass). For example, our library has the book conecept class Books. Now we will make a specific class for a genre of book.
Quote:

class ProgrammingBooks < Books
def.to_s
"Books: #{@title}--#{@year} (#{@author}) #{language}"
super(title, year,author )
@lang = language
end
end
Right there, we made a class category called Programming Books. So If the program were to see a programming book, it would use this subclass. We also used our basic variables, and added a new class specific one, language.

Note:
< Tells ruby that ProgrammingBooks is a subclass of Books.
Quote:

Books = ProgrammingBooks.new("Learning C++", "bulio", "C++")
Books.to_s
That puts Learning C++ into the programming category. It also gives us information about the book via Books.to_s

**Note:**

You can get info using things such as Book.name etc. what ever comes after the . is a variable that you've set.

Cerulean Aug 11th, 2005 3:20 PM

Should the line
:

book1 = Book.new("Ruby Guide","2005","bulio")
not be
:

book1 = Books.new("Ruby Guide","2005","bulio")
Other than that, pretty nice high level intro.

edit: Maybe fix the indenting also?

coldDeath Aug 11th, 2005 3:28 PM

Nice that tutorial is clear and understandable, i don't know any Ruby, but this has shown me what it is like. I would hve tested it all out, but atm i am learning python GUI and network programming, so i would probably get confused and muddled up lol.

Good tutorial.

Infinite Recursion Aug 11th, 2005 3:41 PM

looks good. are you planning on extending this tutorial to cover other areas?

bulio Aug 11th, 2005 4:31 PM

I'll probably cover more as I go along, yes but for now I need to learn more :P

AbhishekKr Oct 29th, 2005 11:32 PM

gud job done m8


All times are GMT -5. The time now is 9:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC