Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[RUBY] Classes Tutorial
04-20-2010, 01:45 PM
Post: #1
[RUBY] Classes Tutorial
Classes in Ruby

In object-oriented programming, a class is a construct that is used as a blueprint (or template) to create objects of that class. This blueprint describes the state and behavior that the objects of the class all share.
An object of a given class is called an instance of the class. The class that contains (and was used to create) that instance can be considered as the type of that object,
e.g. an object instance of the "Fruit" class would be of the type "Fruit".

Starting with classes
Lets look at the skeleton of a ruby class

Code:
class P9
#Do some things here
end

This is very simple code, and speaks for it self.
It constructs a class named P9 code goes in between the
constructor and the end statement.

Adding Variables

A class with class variables
Code:
class P9
@@P9 = 9999
end
@@ is a class variable:
A class variable belongs to the class and is a characteristic of a class.

Creating Objects Derived From Your Class
This is an easy task, especially if you're familiar with C++

Code:
class P9

def initialize(var_1,var_2)#input variables
@random_1 = var_1#Assign these variables to the inputted ones
@another_random = var_2

end

def Display()
    puts "First variable #@random_1"$#Function for printing the variables
    puts "Second Variable is #@another_random"
end

end

Example = P9.new(10,10)#Creates new object
Example.Display()#Displays it

Inheritance
Code:
class P10 < P9#Inherit off of P9
    def Different#Own function
        print "Im Class P10! Different Than P9"
    end
    
    end
    Example_ = P10.new(10,50)#All the previous functions + 1
Example_.Display()
Example_.n()

Thats the end of this short tutorial, I hope it helped anyone tryingto learn Ruby

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
04-20-2010, 10:48 PM
Post: #2
RE: [RUBY] Classes Tutorial
This is great.
I can see Back_track that you are becoming an Ruby programmer.
You can add some more tuts !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
04-21-2010, 05:56 AM
Post: #3
RE: [RUBY] Classes Tutorial
Yeah, I'm now trying to make a chat program in Ruby. But it's confusing me, especially coming from C#. For some reason, Chat was very easy to me in Delphi and C#, this may be a challenge in Ruby.

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: