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 = Array.new
  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 = Array.new(20)
  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 = Array.new(10,'harish')
  19. # print array
  20. puts array3

Output:

  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}"

OUTPUT:

  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)

FunctionDescription
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 :)

Output:

  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

[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Ruby is a pure object oriented, general purpose,interpreted and server side scripting and programming language. Ruby ...
Introduction to Ruby
In this step by step free tutorials we will learn about printing, comments, puts, print, begin, end and EOF methods f...
Getting Started with Ruby
In this tutorial of free step by step free tutorials series we read about variables, constant, printing in staring an...
Variables in Ruby

Login or Sign up to leave comment.