In this challenge, we will show you ways in which we can add key-value pairs to Hash objects, delete keys from them, and retain them based on a logic. Consider the following Hash object: A new key-value pair can be…
You've seen the control structure each used on an array. Similarly, it is available for the Hash collection, as well. On Hash, it works in two ways. Consider the example Using each, each element can be iterated as or…
Hashes, also called associative arrays, are dictionary-like data structures which are similar to arrays. Instead of using integers to index an object, however, hashes use any object as its index. In this challenge, your…
The array class also allows to select and return a subset of an array based on some criteria defined in a block (a block is a group of code within {} that accepts a variable and returns a value). Selecting elements that…
The array class has various methods of removing elements from the array. Let's look at the array arr = [5, 6, 5, 4, 3, 1, 2, 5, 4, 3, 3, 3] Delete an element from the end of the array > arr.pop => 3 Delete an element…
Arrays provide a variety of methods that allow to add elements to them. push allows one to add an element to the end of the list. >x = [1,2] >x.push(3) => [1,2,3] insert allows one to add one or more elements starting…
Here are some other ways to access array objects in Ruby. To access the elements from the end of the list, we can use negative indices. For the array, arr = [9, 5, 1, 2, 3, 4, 0, -1] > arr[-1] => -1 The first element of…
Array collections offer various ways to access their elements. The positions are 0 indexed. Objects of the array can be accessed using the [] method which may take various arguments, as explained below. arr = [9, 5, 1…
One of the most commonly used data structures in Ruby is a Ruby Array, and below we see various methods of initializing a ruby array. Your task is to initialize three different variables as explained below. Initialize…