rubyonrails

Today begins a series of articles that describe the main aspects of the Ruby language and which will focus on Rails (the most widespread and powerful Web Application Framework of the language) which implements the MVC pattern, but which also contains a very interesting ORM tool. The intent is to touch on various aspects, from the syntax of the Ruby language to the use and coding of the framework, highlighting its peculiarities, merits and possibly even limits.

Introduction

This is an interesting technology on the international scene that deserves, in our opinion, a careful analysis by those who want to have a complete picture of the opportunities available in the “application development” field.

A lean, productive, portable and free choice “… free of charge, but also freedom to use, copy, modify, and distribute it …”.

In our series of articles we intend to touch on various aspects, from the first steps of configuring the environment, to web development with the Rails framework, passing through Unit Testing, Duck Typing and other features of the language such as Regular Expression, Modules, etc.

A Bit of History

Ruby is a language with unique characteristics. The most interesting perhaps is the fact that it is mostly the result of the work of one person, the Japanese Yukihiro Matsumoto, or more confidently “Matz” who started developing the language since 1993, so well before Java appeared. on the horizon (it was still an internal Sun project called Oak at the time).

Subsequently, as Java spread like wildfire, propelled by Sun and the spread of the World Wide Web, Ruby grew slowly, gradually winning over a host of passionate users. In the last year, the number of Ruby-based projects has grown considerably, thanks also to the spread of the Rails framework, which proposes a drastically simplified (and above all more productive) web programming paradigm compared to mainstream Java programming for the web.

Rails aside, it is interesting to note that Ruby is currently considered the most enjoyable language to program in. The consistency of the syntax, its cleanliness and its intrinsic simplicity are in fact the characteristics that are most often praised by Ruby programmers.

The first “hello world” program in Ruby comes to light in the summer of 1993. The first “alpha version” is released in December 1994.

From that moment a considerable interest in the language began, so much so that it is currently one of the few examples of software technology coming from the East and which has established itself all over the world.

ruby rails

Why Ruby

Suppose we want to write solid and easily maintainable applications (especially web applications).

We have various alternatives to get around. Let’s try to evaluate them together.

Java, in its various interpretations in the form of frameworks such as Struts, Java Server Faces or Tapestry, is certainly a valid and viable option. The weak point of most of the presentation frameworks in Java is however given by the learning time necessary to be productive, for a number of reasons:

  • frameworks evolve, often faster than the support that can be offered by the IDE
  • often the framework changes from project to project
  • frameworks are complex and require familiarity with the MVC pattern, with XML, HTML, JSP, Tag Libraries, Servlet, etc.

The result obtained would be a solid application, but, probably, we would not get it smoothly and quickly. Furthermore, the maintenance cost would be not negligible.

In the event that the context requires the use of J2EE applications of a certain complexity, the choice would be almost obligatory in the use of Java Enterprise. In other situations we can evaluate further possibilities.

In order to reduce the initial development time we could opt for PHP.

The systematization of the code in a scripting language of this type (even if OO with PHP5), almost devoid of valid MVC frameworks, combined with the incomplete availability of tools to support the various development phases (which necessarily require self-discipline in the various stages of the process) make us carefully consider this choice.

Or in any case they make us relegate this choice to some particular situations such as the extension of the myriad of Open Source applications available.

We could opt for certainly much less common alternatives, and not really Object-Oriented, such as Phyton or Perl. Or … at this point we could consider seriously considering the Ruby hypothesis.

One of Ruby’s strengths is undoubtedly speed of development and ease of maintenance

Mike Clark’s words about “… programming in any language other than Ruby will feel like you’re pushing the rope” testify to the ease of programming in Ruby.

In the following we will try to provide exhaustive explanations and to explain the reason for this statement.

ruby rails development

Characteristics of the Language (from Java Developer)

Ruby is an Object Oriented language. Simple language to understand and program.

Whatever is manipulated in Ruby is an object and the same is true of the result of the manipulation.

For example, the number 1 is an instance of the Fixnum class.

A Java developer should be quite comfortable with Ruby as it is an OO language with a number of features that make it feel particularly comfortable, (we are referring to the presence of classes, inheritance, instance and class variables , etc.). However, what must be removed from the mind of the Java programmer is the temptation to use a Java-like coding style, which, especially at the beginning, turns out to be a rather strong attraction.

Before moving on to the coding phase, let’s do a brutal, and obviously not exhaustive, list of Ruby features in a Java comparison perspective. This will help us to frame it having as a starting point the knowledge of Sun’s language.

Assuming that Java is a mature, tested but also very “verbose” language, what you will notice quite quickly when switching to Ruby is the drastic reduction of the lines of written code.

We list the main characteristics that unite or differentiate the two languages.

Ruby, like Java

  • it is an interpreted language
  • is an OO (Object Oriented) language
  • has a garbage collector for memory management
  • it is portable and can be used with the main operating systems
  • has strongly “typed” objects
  • has a source documentation tool (RDoc) very similar to JavaDoc
  • has public, protected and private “accessories”, even if their meaning is different from the most common OO languages ​​and Java

Unlike Java

  • the code does not need to be compiled.
  • there is no type declaration
  • the keyword end is present to terminate classes, methods and structures in general
  • there are no primitives
  • there are no interfaces, but it has mix-in
  • methods cannot be overloaded
  • but above all it is “dynamically typed”. and we will see later what the impact of this last feature is.

Let’s keep the above in mind and let’s move on to the installation of the environment, a preliminary operation to get to the coding.