RPG Maker VX Ace Wiki
Register
Advertisement

In Ruby, variables are, unlike other programming languages, not bound to a specific type. Instead, their type is changed automatically depending on the variable's content.

To demonstrate this, let's compare variables in Java and Ruby.

Java:

int a = 5; //variable "a" contains an Integer, 5.
a = "testing.."; //a is set to "testing..", a String.
//Since a is an Integer, the line above will produce an error.

Ruby:

a = 5 #variable "a" contains 5, an Integer.
a = "testing.." #a is set to "testing..", a String.
#a is automatically changed from an Integer to a String.
#It now contains "testing.." and no error occurred.

As you can see, this greatly simplifies your work. It's not even necessary to declare a variable!

There are several different kinds of variables.

Local Variables[]

Local variables are, as the name suggests, local. This means that they only exist inside the scope they are declared in.

def world
  txt = "Hello World"
  print(txt) #print what is contained in txt
 #=> "Hello World"
end

In this example, we created a method world. Inside this method, a variable, txt, is created and set to a value ("Hello World"). After that, the variable is printed, resulting in "Hello World".

This works because print is used inside the scope txt is declared in.

However, if it is not within that scope, it won't work:

def world
  txt = "Hello World"
end

print txt #=> error

In this example, print cannot find txt, resulting in an error. This is because txt only exists within the world method and print is used outside of that method.

Instance Variables[]

Instance variables exist within the scope of a specific "instance" of an object. Each time a new object is created, it gets its own set of instance variables. Instance variables have an "@" symbol at the beginning of their names.

class Example
  
  #This method is called every time a new instance is created
  def initialize
    @a = 0 #This is an instance variable
  end
  
  def add_to_x
    @a += 2 #Add 2 to @a
    print(@a)
  end

end

#Now we create an instance of Example and call the method
exam = Example.new
exam.add_to_x

Note that instance variables cannot normally be referenced from outside this instance. However, if attr_reader, attr_writer, or attr_accessor is used, these can be accessed from outside the instance.

exam.a = 5 #=> Error

Global Variables[]

Global variables have a "global" scope - they can be referenced from anywhere. Global variables have a dollar sign ($) before their names.

$global_var = "Hello World!"

Constants[]

Constants are variables that are "frozen" - after they are assigned a value, they cannot be changed. Constants can be referenced from inside a class, as shown below; if they are not in a class, then they can be accessed from anywhere.

CONSTANT = 5
CONSTANT += 1 #=> Error

class Example
  CLASS_CONSTANT = 3 #Constant exists within a class
end

#Accessing a constant from within a class
print(Example::CLASS_CONSTANT)
Advertisement