Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Ruby—A Diamond of a Programming Language? : Page 3

Ruby is an object-oriented, meta-programming language that has many developers wondering if there are actually better alternatives to languages like Java and C#.


advertisement
Instance Variables and Attributes
Classes can also have instance variables (also known as properties in some languages). For example, objects created from the Rectangle class should all have a height and width. In Ruby, instance variables do not explicitly have to be declared in the class, they simply have to be used and annotated with a special character in their name. Specifically, all instance variable names begin with an "@." To store the height and width in the instance of rectangle when the area method is invoked, you merely have to add instance variables to the area method:

class Rectangle def area (hgt, wdth) @height=hgt @width = wdth @height*@width end end

More appropriately, when an instance of Rectangle gets created, the height and width should be specified and the instance variables set at that time. Ruby provides a special method called initialize to allow you to set up or prepare new instances of the class:

class Rectangle def initialize (hgt, wdth) @height = hgt @width = wdth end def area () @height*@width end end

To create a new object or instance of Rectangle, you call on the standard Ruby constructor method "new" against the class—in this case, Rectangle:


Rectangle.new(4,7)

Or, if you prefer the same instantiation without parentheses, although again for clarity's sake, I would recommend using parentheses:

Rectangle.new 4,7

In this example, a new Rectangle object will get created, and the initialize method is called with the parameters of 4 and Rectangle class also defined methods for granting access. Notice, in the code below, the height and width methods have been added in order to share height and width information:

class Rectangle def initialize (hgt, wdth) @height = hgt @width = wdth end def height return @height end def width return @width end def area () @height*@width end end

Likewise, in order for some other object to update or set the height and width of a Rectangle object, additional mutating methods need to be defined:

class Rectangle def initialize (hgt, wdth) @height = hgt @width = wdth end def height return @height end def height=(newHgt) @height=newHgt end def width return @width end def width=(newWdth) @width=newWdth end def area () @height*@width end end

The mutator methods above ("height=" and "width=") may look tricky, but they really are just methods. Don't let the equal sign in the name fool you. The additional character at the end of the method name means nothing to Ruby, but it makes it seem more readable to humans when used. What do you think the following code looks like it is doing?

aRectangle.height=27

That's right, a Rectangle object's height is being assigned/changed. In fact, it is just a call to the method "height=" on the Rectangle object.

Because granting access to an object's instance variable is quite common, Ruby comes with a set of keywords that essentially define the instance variable and the accessor/mutator methods all at once; making these instance variables "public" attributes. The keywords are attr_reader, attr_accessor. They help simplify the code considerably:

class Rectangle attr_accessor :height, :width def initialize (hgt, wdth) @height = hgt @width = wdth end def area () @height*@width end end

In the example code above, attr_accessor gives the Rectangle both getters and setters for the height and width attributes.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap