OmegaDev
Would you like to react to this message? Create an account in a few clicks or log in to continue.

IMP1 Ruby Tutorial

Go down

IMP1 Ruby Tutorial Empty IMP1 Ruby Tutorial

Post by IMP1 Wed Oct 27, 2010 6:22 pm

First off, download the Ruby Installer. It'll allow you to try out ruby by yourself.

Ruby Tutorial V - Classes

Things to know:
The way that can be described is not the right way.


First off, in Ruby, everything is sorted out into Classes. These can be as simple as:
Code:
class Any_Name_starting_With_A_Capital
  def initialize
    puts "Hello World" # Cliché I know. But comforting :)
  end
end
Now let's take a quick-quick look at what we have. We make a class and give it a name, then we define the initialize method. This is Ruby's in-built method that, when a class is called, is done first. Any methods we define within a class are only usable by instances of that class (or of sub-classes - another thing for another time). You get that? We can define our own methods! Here I defined the initialize method to make it put "Hello World". So I've made a class that will do this, great. But, well... when? I need to call an instance of the class! Like so:
Code:
any_variable = Any_Name_starting_With_A_Capital.new
Now, this creates that class and, because initialize is defined, it runs it. If we had a different class, like I don't know, this one:
Code:
class IMP1_Super_Class
  def poke
    puts "*squeal*"
  end
end
If I were to create an instance with a = IMP1_Super_Class then nothing would happen, because there's no initialize method, but now I can use any method from the class on that instance. So i could use a.poke and it would puts "*squeal*".

Now, back to the different types of variables, since we now know what classes are. There are:

Local Variables
These are only used in a method (or block). They start with a-z or an underscore (_).

Instance Variables
These can be accessed throughout a class. They start with an at sign (@).

Class Variables
These are accessed throughout every instance of the class, and they must be initialized in the class (outside of a method, usually before initialize). If it is changed then it is changed for every instance of the class. They start with dual-at signs (@@)

Global Variable
Global variables in Ruby are accessible from anywhere in the Ruby program. However, Use of global variables is strongly discouraged. The problem with global variables is that, not only are they visible anywhere in the code, they can also be changed from anywhere. This can make tracking bugs difficult. They start with a dollar sign ($).

Okay, they all are pretty straight forward. All that is, except the class variables, I always had the hardest time thinking when they'd ever be useful. In this next example I'll not only use them I'll also use a Sub Class. Take a look:
Code:
class Car
  @@wheels = 4
  @@cars_produced = 0
  def initialize
    @@cars_produced += 1 # a += 1 is the same as a = a + 1. It works with - * / % too
  end
  def drive
    puts "vroooom!"
  end
  def self.amount        # see *1
    return @@cars_produced
  end

end

class Strecth_Limo < Car # Using Class1 < Class2 means Class1 has everything Class2 has,
                        # but you can add specific Class1-only stuff too.
  @@wheels = 8
  def drive              # note that Car already has this class, but I've overwritten it.
                        # Methdods in Sub Classes overwrite their parent classes.
    puts "chugga chugga"
  end
  def turn_on_TV
    puts "done."
  end
end
*1 This method is different. It's a self.method_name method. This means it's called on the class, rather than any instance. So you'd call Car.amount instead of a = Car; a.amount. The second might give you an error! And nobody wants that.


Homework:
Make sure everything I've said so far makes sense and, if any of it doesn't, read through again and then ask for clarification.


Good Hunting.

NEXT TIME: Arrays
IMP1
IMP1
Coding Moderator
Coding Moderator

Posts : 503
Gald : 1399

Stats
Cookies: 5

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum