Summary of ruby methods for creating arrays

Time:2022-5-11

Programs often need to manage variable sets. For example, the program that manages the calendar must have a list of days of the week. They must be stored in a variable every day, and their list can be stored in an array variable. Through 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 must populate it with other variables to use it. This is a common way to create variables if you want to read a list of contents 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 (an ordered sequence of characters) from the keyboard and “pushed” or add them to the end of the array.

?
1
2
3
4
5
6
#!/usr/bin/env ruby
array = Array.new
3.times do
str = gets.chomp
array.push str
end

Use array literals to store known information

Another use of arrays is to store a list of what you already know when you write 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 as in the previous example, but there is a simpler way. You can use array text.

In programming, “literal” is a variable type built into the language itself. It has a special syntax to create it. For example, 3 is a numeric literal and “Ruby” is a string literal. Array text is a list 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 of days of the week and prints it out. Use array literals and use each loop to print them. Note that each is not built into the ruby language, but a function of array variables.

?
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env ruby
days = [ "Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
]
days.each do|d|
puts d
end

Use index operators to access individual variables

In addition to simply looping through 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 takes a number and retrieves a variable from the array whose position in the array matches the 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 list of names 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 variables in the array.

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

Recommended Today

XML grammar rules

version: XML default version number, this attribute must exist encoding: The encoding of this XML file Other Components of XML Annotation information can be defined in the XML file: The following special characters can exist in an XML file <   &gt; &gt; greater than &amp; &amp; ampersand ' ' apostrophe &quot; &quot; quotation marks A […]