Support Online
Skip to main content

Working with Arrays in Ruby: A Basic Data Management Guide

Arrays in Ruby organize your data by keeping multiple values ​​in a single variable. In this guide, you will learn step by step how to create, access, add, delete, modify and iterate with arrays.

🧠 What Will You Learn in This Guide?

  • Array creation methods in Ruby ([], %w{})
  • Access to elements (index, first, last)
  • Adding elements (push, <<, unshift)
  • Element deletion (delete, delete_at, pop, shift)
  • Element replacement
  • Loop over array (for, each, each_with_index)

1️⃣ Creating an Array

Instead of storing each data in separate variables, you can collect them in an array:

baliklar = ["Vatoz", "Levrek", "Palamut"]
print baliklar
# ["Vatoz", "Levrek", "Palamut"]

💬 You can create an array by separating the elements with commas using square brackets ([]).

For string arrays, %w{} offers a shorter syntax:

gunler = %w{Pazartesi Sali Carsamba Persembe Cuma Cumartesi Pazar}

💬 You do not need to write quotes and commas.

It is possible to mix different data types:

karma = ["GenixNode", 10, nil, ["sunucu", "bulut"]]

2️⃣ Accessing Array Elements

Arrays are indexed from zero (0 is the first element):

puts baliklar[0] # "Vatoz"
puts baliklar[-1] # "Palamut"

Learning the number of elements:

baliklar.length # 3

Finding the position of an element:

baliklar.index("Levrek") # 1
baliklar.index("Kalkan") # nil

First and last element:

baliklar.first # "Vatoz"
baliklar.last # "Palamut"

Accessing nested arrays:

ic_ice = [["masa", "sandalye"], ["defter", "kalem"]]
print ic_ice[1][0] # "defter"

3️⃣ Adding Elements to an Array

Adding by direct index:

baliklar[3] = "Hamsi"
# ["Vatoz", "Levrek", "Palamut", "Hamsi"]

💬 The spaces you skip will be nil.

Adding to the end (push or <<):

baliklar.push("Kefal")
baliklar << "Lüfer"
# ["Vatoz", "Levrek", "Palamut", "Hamsi", "Kefal", "Lüfer"]

Prefix (unshift):

baliklar.unshift("Dil Balığı")
# ["Dil Balığı", "Vatoz", "Levrek", "Palamut", "Hamsi", "Kefal", "Lüfer"]

4️⃣ Deleting Elements from the Array

Removing element at a specific index:

baliklar.delete_at(0)
# ["Vatoz", "Levrek", "Palamut", "Hamsi", "Kefal", "Lüfer"]

Deleting specific value:

baliklar.delete("Hamsi")
# ["Vatoz", "Levrek", "Palamut", "Kefal", "Lüfer"]

Delete from end (pop):

baliklar.pop
# ["Vatoz", "Levrek", "Palamut", "Kefal"]

Wipe from the beginning (shift):

baliklar.shift
# ["Levrek", "Palamut", "Kefal"]

💬 pop removes elements from the end, shift removes elements from the beginning


5️⃣ Element Update

Changing a specific element:

baliklar[0] = "Mezgit"
print baliklar
# ["Mezgit", "Palamut", "Kefal"]

6️⃣ Looping (Iteration) in Arrays

In Ruby arrays, loops are used to process elements one by one.

for..in structure:

for balik in baliklar do
puts balik
end

each method:

baliklar.each do |balik|
puts balik
end

Single line form:

baliklar.each { |balik| puts balik }

Loop with index (each_with_index):

baliklar.each_with_index do |balik, index|
puts "Konum: #&#123;index&#125;, Değer: #&#123;balik&#125;"
end

💬 Frequently Asked Questions (FAQ)

1️⃣ Should I use Array or Hash? If your data is an ordered list, use array, if it has a key-value relationship, use Hash.

2️⃣ What is the difference between push and unshift? push adds to the end, unshift adds to the beginning. unshift shifts other indexes.

3️⃣ Why do Nil values ​​occur? If you add elements by skipping, nil is placed in the empty indexes.

4️⃣ How to completely clear the array? You can reset the array with the dizi.clear command.

5️⃣ What does each_with_index do? It returns both the value and index of the element at the same time.


🎯 Result

In this guide, you learned the basic building blocks of Ruby arrays: array creation, access, insertion, deletion, update, and loop navigation.

💡 Try: Practice these examples by setting up a Ruby environment on the GenixNode platform.