Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - ARRAYS


An Array in Ruby is a Collection that holds multiple values, of different Data Types. And in an Array the elements are ordered (We will explain it soon), the values are changeable and allows duplicate values.


There are two ways by which you can declare an Array.

  1. You can place multiple values inside square brackets [] and Ruby will understand that it is a Array.

    x = [5, "John", "Ruby"]

  2. You can use the new method on Array class and specify the size of the Array.

    arr = Array.new(10)

Let us see the first way of creating Array, i.e. Using square brackets [].


Creating a 'Array' with values of different Data Types


Example :



x = [5, "John", "Ruby"]
puts x 


Output :



  5
  John
  Ruby

So, in the above code we have created a Array using square brackets [].


And put an Integer type value (i.e. 5) and two String type value (i.e. John and Ruby)


x = [5, "John", "Ruby"]

And initialised to the variable x.

java_Collections

So, we can see that two different data types are assigned to a Array.


In the next line we have printed the Array using the puts statement.


puts x

Now, if we see the output,

Output :



  5
  John
  Ruby

Iterating a Array using for loop


Example :



x = [5, "John", "Ruby"]
for i in x
	puts i
end


Output :



  5
  John
  Ruby

Similarly, in the above code we have created a Array using square brackets [].


x = [5, "John", "Ruby"]

And initialised to the variable x.

java_Collections

In the next line we have used the for loop to Iterate through the Array.


for i in x
	puts i
end

Now, if we see the iterations of for loop,


for i in x

1st Iteration


In the first Iteration the first value of the Array x (i.e. 5) is taken and put into the variable i.

java_Collections

And the print statement, prints the value of i.

Output :



  5

2nd Iteration


Similarly, in the second Iteration the second value of the Array x (i.e. John) is taken and put into the variable i.

java_Collections

And the print statement, prints the value of i.

Output :



  5
  John

3rd Iteration


Similarly, in the third Iteration the third value of the Array x (i.e. Ruby) is taken and put into the variable i.


And the print statement, prints the value of i.

Output :



  5
  John
  Ruby

Now, if you see the final output. You can find that the values of the Array are displayed in the same way they were inserted.


i.e. First 5 is printed, then the name John and finally Ruby is printed.


And if you see the Array,


x = [5, "John", "Ruby"]

It is printed in the same order it was inserted. And this is why a Array is said to be Ordered.


Let us see the second way of creating an Array. i.e. Using the new method.


Creating a 'Array' with new


So far we have seen variables, which is used to store a single value. But what if we need to store 100 values?


We cannot create 100 variables. And to fill this gap Arrays came into picture.


Arrays are used to store multiple values of the same or different type.


Below is the way of declaring an array of size 10:


arr = Array.new(100)

Now, we will be asking Ruby to store numbers 1 to 10 in an array.


Example :



arr = Array.new(10)

for i in 0..9
	arr[i] = i + 1
end	
for i in 0..9
	puts "The contents of the array are : #{arr[i]}"
end


Output :



  The contents of the array are : 1
  The contents of the array are : 2
  The contents of the array are : 3
  The contents of the array are : 4
  The contents of the array are : 5
  The contents of the array are : 6
  The contents of the array are : 7
  The contents of the array are : 8
  The contents of the array are : 9
  The contents of the array are : 10

So, in the above code, we have declared an Array to hold 10 numbers.


arr = Array.new(10)

And used the for loop to store the numbers 1 to 10 in the Array arr.


for i in 0..9
	arr[i] = i + 1
end

So, 10 locations are created in the array and the numbers, 1 to 10 are stored there.

java_Collections

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
arr[6] = 7
arr[7] = 8
arr[8] = 9
arr[9] = 10

Similarly, we have used the next for loop to print the values of the array, arr.


for i in 0..9
	puts "The contents of the array are : #{arr[i]}"
end

Next, let us see, how to access the elements of the Array in the next tutorial.