Ruby Arrays

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby Arrays

Ruby arrays are ordered collections of objects. They can hold objects like integer, number, hash, string, symbol or any other array.

Its indexing starts with 0. The negative index starts with -1 from the end of the array. For example, -1 indicates last element of the array and 0 indicates first element of the array.


Creating Ruby Arrays

A Ruby array is created in many ways.

  • Using literal constructor []
  • Using new class method

Using literal construct []

A Ruby array is constructed using literal constructor []. A single array can contain different type of objects.

For example, following array contains an integer, floating number and a string.

  1. exm = [4, 4.0, "Jose", ]   
  2. puts exm 

Output:

Ruby Arrays 1

Using new class method

A Ruby array is constructed by calling ::new method with zero, one or more than one arguments.

Syntax:

arrayName = Array.new  

To set the size of an array,

Syntax:

arrayName = Array.new(10)  

Here, we have mentioned that array size is of 10 elements.

To know the size of an array, either size or length method is used.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. exm = Array.new(10)   
  4. puts exm.size    
  5. puts exm.length 

Ruby Arrays 2

Example

  1. #!/usr/bin/ruby   
  2. exm = Array("a"..."z")   
  3. puts "#{exm}"

Output:

Ruby Arrays 3

Accessing Array Elements

Ruby array elements can be accessed using #[] method. You can pass one or more than one arguments or even a range of arguments.

 

#[] method

Example

  1. days = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]   
  2. puts days[0]      
  3. puts days[10]   
  4. puts days[-2]     
  5. puts days[2, 3]   
  6. puts days[1..7]   

Output:

Ruby Arrays 4

Output:

Ruby Arrays 5
 

slice method

The slice method works similar to #[] method.

fetch method

The fetch method is used to provide a default value error for out of array range indices.

Example:

  1. days = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]   
  2. puts days.fetch(10)  

Output:

Ruby Arrays 6
 

Example:

  1. days = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]   
  2. puts days.fetch(10, "oops")  

Output:

Ruby Arrays 7

first and last method

The first and last method will return first and last element of an array respectively.

Example:

  1. days = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]   
  2. puts days.first   
  3. puts days.last   

 

Output:

Ruby Arrays 8

take method

The take method returns the first n elements of an array.

Example:

  1. days = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]   
  2. puts days.take(1)   
  3. puts days.take(2) 

Output:

Ruby Arrays 9

drop method

The drop method is the opposite of take method. It returns elements after n elements have been dropped.

Example:

  1. days = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]   
  2. puts days.drop(5)   
  3. puts days.drop(6)  

Output:

Ruby Arrays 10

Adding Items to Array

Ruby array elements can be added in different ways.

  • push or <<
  • unshift
  • insert

push or <<

Using push or <<, items can be added at the end of an array.

Example:

  1. days = ["Mon""Tue""Wed""Thu""Fri""Sat""Sun"]   
  2. puts days.push("Today")   
  3. puts days << ("Tomorrow")   

Output:

Ruby Arrays 11

unshift

Using unshift, a new element can be added at the beginning of an array.

Example:

  1. days = ["Fri""Sat""Sun"]   
  2. puts days.unshift("Today"

Output:

Ruby Arrays 12

insert

Using insert, a new element can be added at any position in an array. Here, first we need to mention the index number at which we want to position the element.

Example:

  1. days = ["Fri""Sat""Sun"]   
  2. puts days.insert(2, "Thursday")   

Output:

Ruby Arrays 13

Removing Items from Array

Ruby array elements can be removed in different ways.

  • pop
  • shift
  • delete
  • uniq

pop

Using pop, items can be removed from the end of an array. It returns the removed item.

Example:

  1. days = ["Fri""Sat""Sun"]   
  2. puts days.pop   

Output:

Ruby Arrays 14

shift

Using shift, items can be removed from the start of an array. It returns the removed item.

Example:.

  1. days = ["Fri""Sat""Sun"]   
  2. puts days.shift 

Output:

Ruby Arrays 15

delete

Using delete, items can be removed from anywhere in an array. It returns the removed item.

Example:

  1. days = ["Fri""Sat""Sun"]   
  2. puts days.delete("Sat"

Output:

Ruby Arrays 16

uniq

Using uniq, duplicate elements can be removed from an array. It returns the remaining array.

Example:

  1. days = ["Fri""Sat""Sun""Sat"]   
  2. puts days.uniq   

Output:

Ruby Arrays 17