A Little Ruby Program
To help you get a better feel for the basics of Ruby, I have included a little application with this article for you to explore. To get the application working, download and unzip the contents of the file onto your file system. It will create an examples folder wherever it is unzipped. There are nine Ruby code files (.rb
files) in the examples directory. The bulk of the code in this article is in these files. Additionally, you will find a testShapes.rb
file which serves as the main kick off point for testing Ruby Rectangle, Square, and Circle objects. Simply bring up a command prompt and use ruby to run the testShapes.rb
file as shown in Figure 8
|Figure 8. A Small Ruby Example: After downloading the zip file, use a command prompt to go to the Ruby source code directory and type ruby testShapes.rb. The results should look something like what is seen in this picture.|
You will notice, in testShapes.rb and some of the other code, that the file begins with "require" and a file name (like rectangle.rb). This is the Ruby notation for bringing in or using code into your Ruby program.
Will Ruby take over Java or C# as one of the leading modern software development languages? I doubt italthough Ruby is supposedly quite popular in Japan. As someone who has been in this industry many years, I am not pessimistic about its chances, but I am pragmatic. I found Smalltalk to be a far superior programming language to Java, but alas, superior does not always win. There is a lot of support behind existing languagesboth technical and marketing. Libraries, toolkits, frameworks, architectures, connectors, adapters, platform support, services, knowledgebase, competent implementation teams, etc. have all been put in place to support programming languages like Java. And, like it or not, marketing machines at Sun and Microsoft will ensure the success of the current development environments for sometime to come.
So why explore Ruby? It could be particularly useful as a scripting language replacement for Perl or Python (which was its original purpose) or for quick prototyping. A few have also seen its power and principals and have adopted Ruby as a great way to teach programming. According to members of my local Ruby users group, a number of people are using it to help test production systems. Beyond that, I invite you, as Bruce Tate and Dave Thomas did me, to explore its power and beauty. Sometimes, we need to be reminded of the way things should be. Even if Ruby does not become a commonly used, supported, and marketed programming language, perhaps with enough awareness and a little use, some of its features will find their way to the programming environments we knowif not lovetoday.
You have enough Ruby help now to get started. There is a lot more to cover, including some very powerful Ruby features such as Ruby's data structures, standard library, Blocks, Regular Expressions, I/O, graphical user interface, and of course, Ruby on Rails (Ruby's Web framework).