Friday, April 3, 2020
Basic Guide to Creating Arrays in Ruby
Basic Guide to Creating Arrays in Ruby Storing variables within variables is a common thing in Ruby and is often referred to as a data structure. There are many varieties of data structures, the most simple of which is the array. Programs often have to manage collections of variables. For example, a program that manages your calendar must have a list of the days of the week. Each day must be stored in a variable, and a list of them can be stored together in an array variable. Through that one array variable, you can access each of the days. Creating Empty Arrays 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 fill it with other variables to use it. This is a common way to create variables if you were to read a list of things from the keyboard or from a file. In the following example program, an empty array is created using the array command and the assignment operator. Three stringsà (ordered sequences of characters) are read from the keyboard and pushed, or added to the end, of the array. #!/usr/bin/env rubyarray Array.new3.times dostr gets.chomparray.push strend Use an Array Literal to Store Known Information Another use of arrays is to store a list of things you already know when you write the program, such as the days of the week. To store the days of the week in an array, you could create an empty array and append them one by one to the array as in the previous example, but there is an easier way. You can use an array literal. In programming, a literal is a type of variable thats built into the language itself and has a special syntax to create it. For example, 3 is a numeric literal and Ruby is a string literal. An array literal is a list of variables enclosed in square brackets and separated by commas, like [ 1, 2, 3 ]. Note that any type of variables can be stored in an array, including variables of different types in the same array. The following example program creates an array containing the days of the week and prints them out. An array literal is used, and the each loop is used to print them. Note that each is not built into the Ruby language, rather its a function of the array variable. #!/usr/bin/env rubydays [ Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday]days.each do|d|puts dend Use the Index Operator to Access Individual Variables Beyond simple looping over an arrayexamining each individual variable in orderyou can also access individual variables from an array using the index operator. The index operator will take a number and retrieve a variable from the array whose position in the array matches that number. Index numbers start at zero, so the first variable in an array has an index of zero. So, for example, to retrieve the first variable from an array you can use array[0], and to retrieve the second you can use array[1]. In the following example, a list of names are stored in an array and are retrieved and printed using the index operator. The index operator can also be combined with the assignment operator to change the value of a variable in an array. #!/usr/bin/env rubynames [ Bob, Jim,Joe, Susan ]puts names[0] # Bobputs names[2] # Joe# Change Jim to Billynames[1] Billy
Subscribe to:
Posts (Atom)