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.
- puts 'initialized array'
-
- # creating array
- array = [1,34,53,53,54]
- puts array
-
- puts 'uninitilized array'
- # uninitilized array
- array1 = Array.new
- # print nothing
- puts array1
- puts 'array with size but uninitialized! 20 empty lines below'
- # creating array with size
- # this array has 20 elements
- array2 = Array.new(20)
- # print free space
- puts array2
-
- puts 'assing same value to all elements'
- # assing value to each element in array
- array3 = Array.new(10,'harish')
- # print array
- puts array3
Output:
- initialized array
- 1
- 34
- 53
- 53
- 54
- uninitilized array
- array with size but uninitialized! 20 empty lines below
- assing same value to all elements
- harish
- harish
- harish
- harish
- harish
- harish
- harish
- harish
- harish
- 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.
- name, age, digit = 'harish',20,34.9
-
- # creating an array
- array = [12,23,name,'string',age,23.09,digit]
- puts "initially array is #{array}"
-
- # array[0] is 12
- puts array[0]
-
- # updata array[0]
- array[0] = 938
- puts array[0]
-
- array[1]+= 898
-
- puts "now array is #{array}"
OUTPUT:
- initially array is [12, 23, "harish", "string", 20, 23.09, 34.9]
- 12
- 938
- 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)
Function | Description |
size | returns the size of the array (called as array.size) |
length | same as size |
at(index) | It takes an argument as index number of the element and returns that element |
clear | remove 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 |
pop | remove last element of the array |
push(object) | add new object to array |
replace(another_array) | replace the content of the array with another array |
reverse | return new array with elements in reverse order of the current array |
- name, age, digit = 'harish',20,34.9
-
- # creating an array
- array = [12,23,name,'string',age,23.09,digit]
- puts "initially array is #{array}"
-
- puts "array size is #{array.size}"
-
- puts "#{array.reverse}"
-
- puts "element at 4th position is #{array[3]}"
-
- # other methods are similar :)
Output:
- initially array is [12, 23, "harish", "string", 20, 23.09, 34.9]
- array size is 7
- [34.9, 23.09, 20, "string", "harish", 23, 12]
- element at 4th position is string