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

Featured pages

Ruby

Set of Ruby Object Oriented Programming Language tutorials that cover such topics as Ruby strings, …

Rails

Rails Framework tutorial teaches you how to utilize de facto framework of choice for Ruby developme…

Ruby Duck Typing

“If an object quacks like a duck just go ahead and treat it as a duck” – this fun…

Regular Expressions

Ruby uses the =~ operator to check any given string against regular expression. For example, a stri…

Credit Card Number

Every last digit of any credit card is a check sum digit that is determined by all digits in front …

Ruby Arrays

Ruby Programming Language has built in support for Arrays. Arrays help you define some of the compl…

Ruby Hashes

Hashes are very similar to arrays in Ruby and hashes interface is similar to Ruby array interface. …

Ruby Code Block

Ruby is very unique language when it comes to code blocks. You can simply pass a code block to a me…