Programming Forums
User Name Password Register

RSS Feed

Thread Tools Display Modes
Old Aug 11th, 2005, 3:39 PM   #1
Hobbyist Programmer
bulio's Avatar
Join Date: Jul 2004
Location: Location
Posts: 141
Rep Power: 14 bulio is on a distinguished road
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.


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.
class Books
def initialize(title,year,author)
@title = title
@year = year
@author = author
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:
book1 ="Ruby Guide","2005","bulio")
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:
class Books
def to_s
"Books: #{@title}--#{@year} (#{@author})"
book1 ="Ruby Guide", 2005," bulio")
That will output:
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.
class ProgrammingBooks < Books
"Books: #{@title}--#{@year} (#{@author}) #{language}"
super(title, year,author )
@lang = language
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.

< Tells ruby that ProgrammingBooks is a subclass of Books.
Books ="Learning C++", "bulio", "C++")
That puts Learning C++ into the programming category. It also gives us information about the book via Books.to_s


You can get info using things such as etc. what ever comes after the . is a variable that you've set.
bulio is offline   Reply With Quote
Old Aug 11th, 2005, 4:20 PM   #2
Professional Programmer
Cerulean's Avatar
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 13 Cerulean is on a distinguished road
Should the line
book1 ="Ruby Guide","2005","bulio")
not be
book1 ="Ruby Guide","2005","bulio")
Other than that, pretty nice high level intro.

edit: Maybe fix the indenting also?

Last edited by Cerulean; Aug 11th, 2005 at 4:50 PM.
Cerulean is offline   Reply With Quote
Old Aug 11th, 2005, 4:28 PM   #3
Expert Programmer
coldDeath's Avatar
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 0 coldDeath is on a distinguished road
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.
coldDeath is offline   Reply With Quote
Old Aug 11th, 2005, 4:41 PM   #4
Infinite Recursion
Programming Guru
Infinite Recursion's Avatar
Join Date: Jul 2004
Location: United States
Posts: 3,508
Rep Power: 17 Infinite Recursion will become famous soon enough
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
looks good. are you planning on extending this tutorial to cover other areas?

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Aug 11th, 2005, 5:31 PM   #5
Hobbyist Programmer
bulio's Avatar
Join Date: Jul 2004
Location: Location
Posts: 141
Rep Power: 14 bulio is on a distinguished road
I'll probably cover more as I go along, yes but for now I need to learn more
bulio is offline   Reply With Quote
Old Oct 30th, 2005, 12:32 AM   #6
AbhishekKr's Avatar
Join Date: Oct 2005
Posts: 5
Rep Power: 0 AbhishekKr is on a distinguished road
gud job done m8
I feel like GOD...
AbhishekKr is offline   Reply With Quote


« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 AM.

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