|Getting Started With Ruby: Object-Oriented Approach|
|Written by Mike James|
|Monday, 25 March 2013|
Page 1 of 3
Ruby is a language that is fun to use. It mixes a number of different programming styles - functional, dynamic and object-oriented. In this first look at Ruby we examine its way of doing object-oriented programming.
Installing Ruby can be done in many different ways. If you simply want to give the language a spin then under Windows the painless way of getting it install is to use RubyInstaller. If you are using any flavor of Linux or Mac OS X then use the Ruby Version Manager RVM - RVMinstallation page.
The installation comes with a full Ruby system an interactive shell, documentation and some utilities. What you don't get is an IDE of any sort. There isn't a single accepted standard Ruby IDE to help you get started but my favorite is JetBrains Ruby Mine. This isn't free but you can get a 30 day free trial to get you started on creating Ruby programs. It is important to realize that it has lots of advanced features that only become useful when you start to build more advanced Ruby applications.
In this article we take an overview of Ruby's approach to objects and working with them. The account isn't complete it is more an attempt to convey the flavor of Ruby as an object oriented language and to point out some of the things that are different from languages such as Java and C#.
However just to make sure everything is working lets do things from the command line first.
Command Line Ruby
To make sure everything is working use NotePad, NotePad++ or any text editor and create a plain text file containing:
Save the file under the name Hello.rb in any directory and run the program by first loading the "Start Command Prompt with Ruby". Then change directory to the location that has Hello.rb stored in it and type:
You should see the hello message appear in the console as puts, i.e. put string, writes to the stdio stream. Also notice that you do need to put the ".rb" extension on the command line. As long as this works you have Ruby installed and working. The Ruby.exe program is the Ruby interpreter. There is also IRB which provides an interactive environment for the interpreter.
What is Ruby?
The big problem with coming to terms with any new programming language is trying to find out its essential character. It is all too common for the expert in the language to point out the minor syntactic decorations that make something very specific into something very easy.
Such "gadgets" are always handy but they generally don't give you the big picture of the idea or philosophy that has brought yet another programming language into the world.
Ruby can be described as a cross between Smalltalk and Perl, which if you know either language is a strange prospect. Smalltalk is a pioneering object oriented language that popularized many of the object oriented approaches we take for granted today but usually in a watered down form compared to the original. Perl is a scripting language with enough bolted-on features to make it possible for an expert to write very condensed scripts which even another expert can find difficult to decode.
Mixing these two languages together clearly has the potential to create something that is a mess, but the creator of Ruby, Yukihiro Matsumoto, decided that the Principle of Least Surprise was to be the guiding light.
That is the language should as far as possible work as you would expect it too. Of course this is very subjective - what is surprising to a C# programmer will be expected by a Perl programmer.
In practice all you can really say is that Ruby tries to be transparent about what it means - whether it actually succeeds in this aim is something that will be argued about well into the language's maturity.
Ruby is object oriented and the first lesson is that in Ruby everything is an object, even those things that would be primitives or otherwise made exceptions for reasons of efficiency.
For example, even a numeric literal has methods:
returns the number of bytes needed to store the value and:
returns the length of the string.
You can, of course, define your own objects and Ruby implements a fairly class-based approach to object creation - but, as is the tendency in dynamic languages, a Ruby Class is just as much an object as any other object.
That is a class is just an object, i.e. an instance of Class, with some special methods that will make further instances and methods that work with instance data.
For example, to create a point class you simply write:
This creates a constant called Point - any identifier beginning with a capital letter is defined to be constant - and assigns to it a new instance of class. It makes sense to use a constant for a class definition after all you don't want it to be changed at runtime.
You need to be aware that Ruby is one of those languages that does make use of variable naming conventions:
cap = Constant and classes have to be constant
This new class has no useful methods except for new which creates a new instance of the new class, i.e. a new Point. You can already write:
but all this really gives you is a new instance of the class object with no additional usable methods.
Also notice that you don't need to declare variables in Ruby - just using a variable is enough for the system to know that it needs to create it. In this case p is declared by being assigned the new instance.
The next step is to customize the class object to add some instance methods and variables.
The role of a constructor is played by the initialize method:
When you use new it automatically calls initialize and passes on any parameters you specify. Instance variables are denoted by the @ prefix and new copies are automatically created by the new method for each instance as it is created. That is every instance of Point gets its own @x and @y.
Instance variables can be acessed within the methods of an object. You also don't need to declare them within the body of the class - this creates a class instance variable - see later.
Also notice the use of parallel assignment
which assigns x to @x and y to @y this is a common Ruby idiom.
Also notice the way a function is defined using def and end.
So for example if you wanted to add a method that returned the sum of the co-ordinates you would write:
So instance variables are accessible from within instance methods.
|Last Updated ( Monday, 29 April 2013 )|