Ruby - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby Arrays

Ruby Array

shape Introduction

This chapter demonstrates about the Ruby Array which are ordered collection of indexed integer. In which each elements is associated with index and following are the concepts covered in this chapter.
  • Arrays
  • Nested Arrays
  • Common Array Methods

Arrays

shape Description

Ruby arrays are make requested, indexed integer collection of objects. Every element is connected with and these are referred by an index. Array index begins position at 0, just like C or Java. A negative index is accepted with respect to the end of the array - which is, a list of negative - 1 demonstrates the final element of array, - 2 is the beside of final element in given array. Ruby array can hold objects, for example Integer, String, Fixnum, Symbol, Hash, other Array objects. Ruby array is not as inflexible like arrays in different languages. Ruby arrays develop consequently adding components to them. User can create or intialize an array which is created by using new class method is shown below. [ruby] names = Array.new [/ruby] User can also intialize the array size at the time of creating array the code below demonstrates the assigning the values to each element in the array as shown. [ruby] names = Array.new(20) puts names.size # This returns 20 puts names.length # This also returns 20 [/ruby] Result By running the above code in command prompt user can get the following output as shown in below image. Modifying Arrays User can add or remove anything from an array for which there are few methods which helpful to perform some operations if user need to add last items of an array then the pop method can be used. If pop method is called then original array variable was modified but the return value is the popped element as shown in below code. [ruby] irb :007 > array.pop => "another string" irb :008 > array => [1, "Bob", 4.33] [/ruby] And also add the item back can be added to the array and the push method can be used to send the parameters which is shown in below snippet. [ruby] irb :009 > array.push("another string") => [1, "Bob", 4.33, "another string"] [/ruby]

Nested Arrays

shape Description

One can also creates arrays creating the nested array which means array with in the array is known as the nested array and these arrays can be found by using the index. user will also have an array of hashes too. The code below demonstrates the nested array as shown below. [ruby] irb :001 > teams = [['Joe', 'Steve'], ['Frank', 'Molly'], ['Dan', 'Sara']] => [["Joe", "Steve"], ["Frank", "Molly"], ["Dan", "Sara"]] [/ruby] Comparing Arrays In ruby can also compare the arrays for equality using the == operator one can use the unshift method and pop method, the code below demonstrates the comparing Arrays as shown. [ruby] irb :001 > a = [1, 2, 3] => [1, 2, 3] irb :002 > b = [2, 3, 4] => [2, 3, 4] irb :003 > a == b => false irb :004 > b.pop => 4 irb :005 > b.unshift(1) => [1, 2, 3] irb :006 > a == b => true [/ruby] to_s By using the to_s method user can create a string representation array. Ruby behind the screen when using string interpolation to array printing which is shown below. [ruby] irb :001 > a = [1, 2, 3] => [1, 2, 3] irb :002 > "It's as easy as #{a}" => "It's as easy as [1, 2, 3]" [/ruby]

Common Array Methods

shape Description

Ruby introduced some common Array methods which have some built in Array classes in which some classes are listed below. include? Which is used to check whether the given argument is included in the array or not and which have question mark at the ending which means it returns the boolean value at the end the code below demonstrate the include? as shown. [ruby] irb: 001 > a = [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] irb: 002 > a.include?(3) => true irb: 003 > a.include?(6) => false [/ruby] each index Each index iterates through an array just like the each method whenever variable represent the index number which opposed to the value at each index which passed through the index of the each element into block which shown in below code. [ruby] irb: 001 > a = [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] irb: 002 > a.each_index { |i| puts "This is index #{i}" } This is index 0 This is index 1 This is index 2 This is index 3 This is index 4 => [1, 2, 3, 4, 5] [/ruby] sort The sort method is a handy method to order an array and which resulted sorted array and the code below demonstrates the sorted array as shown. [ruby] irb :001 > a = [5, 3, 8, 2, 4, 1] => [5, 3, 8, 2, 4, 1] irb :002 > a.sort => [1, 2, 3, 4, 5, 8] [/ruby]

Summary

shape Key Points

  • Sort method returns the sorted array.
  • include? returns the boolean values.
  • Array is an ordered list of elements.