Array in Ruby Programming Language
Ruby Programming Language has built in support for Arrays. Arrays help you define some of the complex data structures without need for your own classes. Ruby arrays start with zero as many other array implementations in other languages.
Creating an array can be done by simply putting comma separated objects inside brackets.
my_array = [1,44,32,’asc’,’ere’]
Ruby provides several shortcuts when creating an array. If you want to create an array of string then you can simply use the following syntax
my_array = %w{asc ere}
Adding value to an array be performed with the help of << operator.
my_array << ‘str’
Accessing elements in an array is done by passing an index. Important to note, an array index starts with zero and can be a negative number. In case when an array has a negative number passed to you, the count starts from the other end of an array.
Size of an array can be extracted by via size method such as my_array.size