Variables, Assignment
Have you noticed throughout the examples shown so far that something is missing in the code? Have you had to type the constants, instance variable, or class variables? The answer is a big NO! It is part of the truly object-oriented nature of Ruby. To give further definition to this point, take a look at plain old variables in Ruby. Up to this point, you have created a lot of
Rectangle instances, but you have not kept them around for too long. Say you wanted to assign a variable to an instance of
Rectangle you create:
myRectangle = Rectangle.new(4,5)
This is perfectly legal code in Ruby and it doesn't need any other line of code to "type" or declare
myRectangle as something that references
Rectangles. After this line is executed, the variable
myRectangle references an instance of a
Rectangle with a height and width of four and five respectively. But it is just an object reference that can be changed at anytime, without regard to the object's type (againeverything in Ruby is an object). So on the next command prompt line, you could just as easily assign
myRectangle to a string:
irb(main):049:0< myRectangle=Rectangle.new(4,5)
=> #<Rectangle:0x587c758 @width=5, @height=4>
irb(main):050:0< myRectangle="Jim's Rectangle"
=> "Jim's Rectangle"
Try that in many other programming languageseven object-oriented languages like Java and watch the compiler errors fly from your IDE.
Variables, instance variables, class variables, and even "constants" are really just object references. The refer to objects, but they are not the objects themselves. Therefore, they can be changed dynamically; even refering to a different type of object.
Because of this flexibility, some conventions have been established in Ruby to help everyone know how a variable is being used in the code. You have already seen one of thesethe @ signused to signify an instance variable. Other variable, method, and class naming conventions are listed below and demonstrated in Table 1 below.
- Local variables and method parameters begin with a lowercase letter.
- Method names begin with a lowercase letter.
- Global variables begin with a $.
- Instance variables begin with a @ symbol.
- Class variables begin with two @ symbols.
- Constants start with an uppercase character (they usually are designated with all caps).
- Class and module names begin with an uppercase letter.
|
Local Variables
|
Global Variables
|
Instance Variables
|
Class Variables
|
Constants
|
Class Names
|
Method Names
|
|
aVar
|
$Var
|
@var
|
@@var
|
VAR
|
MyClass
|
myMethod
|
|
name
|
$debug
|
@lastName
|
@@interest
|
PI
|
Rectangle
|
area
|
Table 1. This table contains examples of the conventions used in Ruby coding, especially as it applies to various types "variables."
Operator Methods
Now, suppose you want to give instances of the Rectangle class a way to merge or be added to another rectangle instance. You could certainly define an additional method called “add,” but the alternative takes advantage of one of Ruby’s truly object-oriented features. You will override the “+” operator to appropriately add two Rectangle instances. The “+” method (as in 4+5) to Ruby is just another method. As just another method, you can give it functionality to meet Rectangle needs. You will define the “+” operator method to add the area of one rectangle to the area of another rectangle.
def + (anotherRectangle)
totalArea = area() + anotherRectangle.area()
Rectangle.new(@height,totalArea/@height)
end
After adding this method to the
Rectangle class, you can add two
Rectangle instance using the
+ method call:
irb(main):001:0> rect1=Rectangle.new(2,3)
=> #<Rectangle:0x58aa688 @width=3, @height=2>
irb(main):002:0> rect2=Rectangle.new(3,4)
=> #<Rectangle:0x58a6ef0 @width=4, @height=3>
irb(main):003:0> rect1+rect2
=> #<Rectangle:0x58a4a60 @width=9, @height=2>
Operator overloading, which is what you just accomplished, is a feature that may be familiar to those that have programmed in Agol, C++, Python, and a few other languages. Again, Ruby is a culmination of the best aspects of many languages.