Working with Arrays in Ruby

Another useful method while working with an array is uniq method of an Array class. It allows you to strip duplicate values from an existing array. You can also prevent an array from getting duplicates by employing to_set method of Enumerable module.

Ruby arrays use two distinct ways for deletions from an array. They are compact and delete methods.

my_array.compact //will removed nulls from the array and rearrange its index.

my_array.delete(10) //will delete all array elements equal to 10.

Reversing an array is another useful property of Ruby arrays. You can simply specify reverse method and an array will get reverse order from the original.

my_array.reverse //reverses an array

my_array.reverse! //reverses and replaces an array with new order

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…