SoFunction
Updated on 2025-03-07

Summary of Ruby's method to create array

Programs often need to manage collections of variables. For example, a program that manages a calendar must have a list of days of a week. It must be stored in a variable every day, and their list can be stored in an array of variables. With this array variable, you can access every day.

Create an empty array

You can create an empty array by creating a new array object and storing it in a variable. This array will be empty; you have to fill it with other variables to use it. This is a common way to create variables if you want to read a list of content from a keyboard or file.

In the following example program, use the array command and the assignment operator to create an empty array. Read three strings (sorted sequences of characters) from the keyboard and "pushed" or add to the end of the array.

#!/usr/bin/env ruby
array = 
 do
str = 
 str
end

Use array literals to store known information

Another use of arrays is to store a list of what you already know when writing a program, such as the day of the week. To store the day of the week in an array, you can create an empty array and append them one by one to the array like the previous example, but there is an easier way. Array literals can be used.

In programming, "literal" is a variable type built into the language itself, and it has a special syntax to create it. For example, 3 is a numeric literal, while "Ruby" is a string literal. Array literals are lists of variables enclosed in square brackets and separated by commas, such as [1,2,3]. Note that any type of variable can be stored in an array, including different types of variables in the same array.

The following example program creates an array containing the days of the week and prints it out. Use array literals and use each loop to print them. Note that each one is not built into Ruby language, but a function of array variables.

#!/usr/bin/env ruby
days = [ "Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
]
 do|d|
puts d
end

Accessing individual variables using index operators

In addition to a simple loop on the array (checking each individual variable in order), you can also use the index operator to access individual variables from the array. The index operator will take a number and retrieve a variable from the array whose position in the array matches that number. The index number starts at 0, so the index of the first variable in the array is 0.

For example, to retrieve the first variable from an array, you can use array [0], and to retrieve the second variable, you can use array [1]. In the following example, the name list is stored in an array and retrieved and printed using the index operator. The index operator can also be used in combination with the assignment operator to change the value of a variable in the array.

#!/usr/bin/env ruby
names = [ "Bob", "Jim",
"Joe", "Susan" ]
puts names[0] # Bob
puts names[2] # Joe
# Change Jim to Billy
names[1] = "Billy"