If defined in simple words, array is just collection of objects. Arrays are used to store data in them. The most amazing thing about arrays is that, that it can store all type of data in itself. Like suppose we want to have a collection of item where we would like to store details of a person, say their id, name, age, gender, job, salary and so on, it is clear that the data is all of different type, like the id will be number with data type long, then name will be a string, age will be an integer, gender will be a character, job will again be a string and then salary will be float type data, and we want to store it all in one container. To suit our requirement we can use an array, an array can store all type of data in itself, be it same data type or different, also an array can even store other arrays inside it. Each entity in an array is called its element. Arrays are used not only to store and display data, they also support a number of operations that can be performed on them. Arrays can be sorted, merged, modified, deleted and traversed. Apart from operations that can be performed, arrays also help in searching elements, inserting elements and deleting elements.
Due to the vast scope, ease of usage and operations supported on arrays, it is almost impossible to have a software project without arrays.
Suppose we practically need to store a data of, say, 100 people. Are we going to use 100 variables? Answer is No. To handle such situations, almost all the programming languages provide the concept of array. An array is a data structure, which is used to store a collection of data.
As in our recent project, Chivalry, we have used arrays multiple number of times to store and display the data. In the website, there are pages on which it is required to display the data of matching requirements to the user, it is definitely done using the array in Ruby on Rails. Let’s take another example of profile, suppose the user wants to open its profile, that means the system should be able to show all the correct details of the selected user, the profile will contain the name, profile image, gender, age, height, description etc., so display the details the data will come in an array and then further formatted to be displayed according to the front-end page view.
In general conversations when we hear the word loop, our mind suddenly redirects to continuity, which indeed infers to the loop we are talking about here. Loops are something that are used to iterate a block of code for required number of times or unless the specified condition is met. Iteration refers to the continuous usage of a segment of the code. Loops are very widely used because they are very efficient and reduce effort with a substantial amount.
Like the loops in any other language, the loops in Ruby are also used to perform the repetitive execution of the specific code block unless the specified condition is met.
Ruby supports the following loops:
Loops are also one of the very important aspect of any project. As I already mentioned that loops make the code efficient as it increases the code reusability. Suppose we have to print an element in one page, 10 times, so there are two ways to do this, first is to write the print code ten times and the other one, the smart one is to write the code one time and run it in loop ten times. Using common sense we can tell that the latter is the better option.
Let’s take a practical example from one of our projects, Chivalry, here we need to show matching results to the user based on their previously added preferences, so the results can definitely be more than one. So to display those multiple results we use the loop to execute the code required number of times to display the matching results.
Usage of loops not only minimises developers’ efforts, it also makes the code shorter and easy to interpret. Because of numerous benefits of loops, they are very widely used in every programming language these days.