Maybe it is because of the english tutorial i used to learn ruby a bit while im german, but for some reason i did'nt really understand what .new does. Why do you need it?
So i was not sure about @ either so i googled it up and found out that @variable are for Classes only while local variables are for method's.
And variables with a $ in front of it is a global variable which can be accessed from anywhere.
But couldnt i just use locals instead of instance variable? Shouldnt they work both just fine?
Edit:
Oh and btw, what does .set_handler do?
And yea, sorry for my bad english 🙂
Use a local variable when you do not want the value to be stored inside the object. For example, if you perform a loop, the loop variable can be local, because you don't care about the value once the loop is finished. These have no special markers in front, like loop=startValue
Use an instance variable when you want each object of that type to remember the value. For example, if you have a Point with an X and Y coordinate, each Point should store X and Y as instance variables, so all Point methods can refer to the proper X and Y values for that object. These have a single @ mark in front, like @x=3
Use a class variable when you want ALL objects of a type to share a value. For example, if you have a counter that indicates how many Points you have, you could store that counter as a class variable in the Point class. These are defined with 2 @ marks in front, like @@point_counter=0
Use a global variable when you want OTHER objects to be able to read and modify a value. For example, if you have a set of Points on a Canvas, and want only one Canvas to exist, you could use a global variable. These are defined with 1 $ mark in front, like $single_canvas=Canvas.new()
The .new method creates an instance of a particular object, by calling that object's initialize method with the parameters after the new, like:
$single_canvas=Canvas.new(640, 480)
This would call this:
class Canvas
# This method is called
def initialize(width, height)
end
end
I hope this helps.
Just for fun, let me explain it my way 😀
.new creates a new "instance" of the Object.
Ruby is the purest (modern) Object-oriented programming language. You work with objects all the time. A party Actor is an object, who has it's own HP and Mana.
Let's say there is a class called Table. This defines how a Table should behave or look like.
If you do: Table.new - then voilá! You just created a Table. In programming you can create as many as you want immediately 😀
Now, Table alone is the blueprint to create a "new" table, but it's not a table on itself. Let me ask you this: Do you have breakfast on a "Table"? Or on a "Blueprint of a Table"? 😛 (I am sure it's a Table! because no one would have breakfast on a piece of paper).
Next: you don't just create tables and throw them away! You store them - in what we call variables.
If you do: my_table = Table.new - you are saving the Table in a variable that has been born for this current "method" or "function", once the execution is over, the variable is lost and gone. Usually.
If you do: @my_table = Table.new - you are saving the Table in an OBJECT variable that will remain there for other "methods" to use later on. This table will persist in time!
You can also look at @variables as "properties".
A Table probably has a variable @legs that equals to 4! (Well, because these Tables have 4 legs).
If you set that a table has legs = 4, you cannot later ask how many legs the table has. Because when the "method" execution finished, the variable is lost. legs won't exist anymore.
If you do @legs = 4, we can always later on ask "how many @legs does this table have?" and the answer will still be 4! 😀
Lastly, $variables are not object variables, but normally called "global" variables. Now, you cannot use these to determine how many legs a Table has. Because the variable is not stored inside an Object, but shared across all of them (if you changed the value somewhere, it will change for everywhere!)
Hope that made sense 🙂
Cheers!