• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Ruby unshift Method – Add Items to Start of Array

Ruby unshift Method – Add Items to Start of Array

October 19, 2022 Leave a Comment

In Ruby, to add items to the start of an array, we can simply use the Ruby unshift method. The unshift method will add an element to the start of the array.

num_array = [1, 2, 3]
num_array.unshift(4);

print num_array

#Output:
[4, 1, 2, 3]

We can also use the unshift method to add multiple elements to an array.

num_array = [1, 2, 3]
num_array.unshift(4,5);

print num_array

#Output:
[4, 5, 1, 2, 3]

When working with collections of data in Ruby, the ability to easily add items or change the collection is important. One such case where we may want to modify a collection is if we want to add elements to an array in Ruby. To add an item to an array in Ruby, we can use the unshift method. The unshift method will add an element or elements to the start of the array.

Below shows a simple example of how we can use the unshift method to add an item to the start of an array in Ruby.

string_array = ["red","yellow","black","blue"];
string_array.unshift("orange")

print string_array

#Output:
["orange", "red", "yellow", "black", "blue"]

Adding Multiple Items to the Start of an Array in Ruby

If we want to add multiple items to an array in Ruby, then we could once again use the unshift method and include multiple items in it. Below is the code to add multiple elements to an array using the unshift method in Ruby.

colors_array = ["red","yellow","black","blue"];
colors_array.unshift("orange","pink","purple")

print colors_array

#Output:
["orange", "pink", "purple", "red", "yellow", "black", "blue"]

Using the prepend Method to Add Items to the Start of an Array in Ruby

Another method we can use to add items to the start of an array is the Ruby prepend method. The prepend method works in the same way as the unshift method, and we can add one or multiple items to an array using it. Let’s see an example of this method.

colors_array = ["red","yellow","black","blue"];
colors_array.prepend("orange")

print colors_array

#Output:
["orange", "red", "yellow", "black", "blue"]

And here is an example of adding multiple items to the start of an array using the prepend method in Ruby.

colors_array = ["red","yellow","black","blue"];
colors_array.prepend("orange","pink","purple")

print colors_array

#Output:
["orange", "pink", "purple", "red", "yellow", "black", "blue"]

Hopefully this article has been useful for you to learn how to use the Ruby unshift method.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Reverse String
  • 2.  Using Ruby to Capitalize First Letter of String
  • 3.  Ruby has_value Method – Check if Hash Has Value
  • 4.  Ruby Round – Round Number to Nearest Integer with the round Method
  • 5.  How to Square a Number in Ruby
  • 6.  Using Ruby to Create List of Prime Numbers
  • 7.  Using Ruby to Convert String to Boolean
  • 8.  Ruby Infinite Loop
  • 9.  Using Ruby to Convert String to Array
  • 10.  Ruby has_key Method – Check if Hash Has Key

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy