Array in Ruby

Array is the collection of the objects. These objects may be variables, class objects, another array, etc. These objects are associated and ordered by index which starts from 0. A negative index is relative index from end point of the array.

Create an array:

Array can be created in multiple ways.

  1. puts 'initialized array'
  2. # creating array
  3. array = [1,34,53,53,54]
  4. puts array
  5. puts 'uninitilized array'
  6. # uninitilized array
  7. array1 =
  8. # print nothing
  9. puts array1
  10. puts 'array with size but uninitialized! 20 empty lines below'
  11. # creating array with size
  12. # this array has 20 elements
  13. array2 =
  14. # print free space
  15. puts array2
  16. puts 'assing same value to all elements'
  17. # assing value to each element in array
  18. array3 =,'harish')
  19. # print array
  20. puts array3


  1. initialized array
  2. 1
  3. 34
  4. 53
  5. 53
  6. 54
  7. uninitilized array
  8. array with size but uninitialized! 20 empty lines below
  9. assing same value to all elements
  10. harish
  11. harish
  12. harish
  13. harish
  14. harish
  15. harish
  16. harish
  17. harish
  18. harish
  19. harish

NOTE: You can consider array as a variable and operations of variables are applicable on arrays.

Accessing array elements:

Array elements are accessed by using their indexes. Each element is a new variable.

  1. name, age, digit = 'harish',20,34.9
  2. # creating an array
  3. array = [12,23,name,'string',age,23.09,digit]
  4. puts "initially array is #{array}"
  5. # array[0] is 12
  6. puts array[0]
  7. # updata array[0]
  8. array[0] = 938
  9. puts array[0]
  10. array[1]+= 898
  11. puts "now array is #{array}"


  1. initially array is [12, 23, "harish", "string", 20, 23.09, 34.9]
  2. 12
  3. 938
  4. now array is [938, 921, "harish", "string", 20, 23.09, 34.9]

Special methods for array:

These methods are called using syntax => array_name.method_name(arguments)

sizereturns the size of the array (called as array.size)
lengthsame as size
at(index)It takes an argument as index number of the element and returns that element
clearremove all elements from array
concate(another_array)Combine elements of another array to current array
delete(object)delete the element which is equal to object
delete_at(index)delete element with index equal to given index
first(n)returns a new array with first n elements of old array
index(object)returns the index of object which is equal to the given object
last(n)returns a new array with last n elements of old array
popremove last element of the array
push(object)add new object to array
replace(another_array)replace the content of the array with another array
reversereturn new array with elements in reverse order of the current array
  1. name, age, digit = 'harish',20,34.9
  2. # creating an array
  3. array = [12,23,name,'string',age,23.09,digit]
  4. puts "initially array is #{array}"
  5. puts "array size is #{array.size}"
  6. puts "#{array.reverse}"
  7. puts "element at 4th position is #{array[3]}"
  8. # other methods are similar :)


  1. initially array is [12, 23, "harish", "string", 20, 23.09, 34.9]
  2. array size is 7
  3. [34.9, 23.09, 20, "string", "harish", 23, 12]
  4. element at 4th position is string

Click on banner below to Learn: PHP with Laravel for beginners - Become a Master in Laravel

About Harish Kumar

Harish, a technical core team member at with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Full-Stack Web Development with React

Best Udemy Courses

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites


Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250