..


Sponsored Links

Syntax alternatives in Ruby

Article written by Claudio Garau

One of the great advantages of a simple and powerful language like Ruby is the ability to achieve the same results in different ways, ie using different syntactic constructs to print the same output or to generate equivalent behaviors.

In this short article we present a simple example based on the cycles and not just the desired result is to create a simple program that can print all the numbers in a range between "1" and "10".

A solution such as the following:

 



 puts 1,2,3,4,5,6,7,8,9,10

 
would seem at first sight very convenient, but if our range, was much larger (say between "1" and "100"), think again, what we're looking for is a script that needs only 2 parameters: the initial values Final (minimum and maximum) that bracket the range.
In this way, the code used can be used again for generating output very "bulky".

Each pemette do we construct an initial value to increase until it reaches a termination value passed as argument to construct, we see an example:

 



 (1 .. 10). Each do | i |



  



 puts the







 end

 
So for every (each) value in the range corresponding figure will be printed until it reaches a value equal to that established in the conditions laid down by the second parameter (in our case "10").
The two vertical bars that enclose the variable increment ("| i |") are used to delimit the variable inside the loop isolating itself from the rest of the code as it represents a value "in itself".

The same result could be achieved by varying the syntax used leggermanete and "save" the amount of code to type:

 



 (1 .. 10). Each {| i | puts i}

 
The return value will be returned by the execution of a statement ("{| i | puts i}") vaolore repeated for each of the range on the basis of two parameters that mark the execution.

An alternative could be based on the use of the loop, as follows:

 



 for i in (1 .. 10)



  



 puts the







 end

 
Again we passed the 2-cycle parameters that define the range of values ​​that the variable "i" can have increased during the 10 iterations necessary, where the value "10" represents the condition that ends the cycle.

But our alternatives are not finished, let's look at this simple line of code:

 



 1.upto (10) {| i | puts i}

 
upto a method for objects that will represent numeric types, the syntax of this method is alternative to that of "for" write "x.upto (y)" is in fact equivalent to type "for (i = x, x < = y, i + +) {...} ".
In practice with "upto" we can get the same result of a cycle without necessarily resorting to a loop, the result is a saving in terms of typing code and resources provided by the system to execute an instruction.

Now let's see one last example:

 



 * puts (1 .. 10)

 
The last line of code proposal is without doubt the easiest of all those seen so far, the simple use of the symbol "*" allows you to print all the values ​​within the range passed as an argument without resorting to the statement puts any cycle or having to specify the call of any method.

In the same category ...
E-Learning
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links