Ruby - Write arr2cols.rb For this problem, write a Ruby script named arr2cols.rb
ID: 3791823 • Letter: R
Question
Ruby - Write arr2cols.rb
For this problem, write a Ruby script named arr2cols.rb that prints an array’s contents as formatted rows and columns. The data is provided by the predefined arrays below: escargot_player_data, employees, and artists. Your script must work with all three arrays.
The number of columns in the data will be consistent within each data set, but each dataset will have a different number of columns. We can the use Array#eachmethod to iterate all of the elements in an array. each will give us each element in the order it appears in the array.
Your script should work with all these three arrays of data:escargot_player_data, employees, and artists.
Array 1: Escargot Players Data The escargot_player data is a multidimensional array with each sub-array containing three strings.
Array 2: Employee data The employees array is a multidimensional array containing rows with six elements.
Array 3: Artist Addresses and Income The artists array is a multidimensional array with 9 elements in each sub array.
3.1 arr2cols.rb code to get you started
Write arr_methods.rb
In this problem, you will use the_string we used in Lab 1 to create an array consisting of all of the characters in that string.
You can create this array of characters with the String#scan method.
To use this strategy with the_string, you would write this code:
Explanation / Answer
# Program:
escargot_player_data = [
# Column names: name, character, and points
['Jim','bullfrog',99],
['Mack the Knife','caterpillar',12],
['Willy','chihuahua',143],
['Trudy','bunny',3],
['Mary Lou','slow loris',1443],
['Sharon Stone','komodo dragon',8888],
]
employees = [
# Name Address City State SSN Telephone
%w(Walter White 123 Happy Home Drive Albuquerque NM 555-66-7777 505-123-
4567 ),
%w(Jesse Pinkman 43 Cloudy Skies Parkway Albuquerque NM 666-12-3456, 505-
888-9999 ),
%w(Gustavo Fring 123 Pollos Boulevard Albuquerque NM 565-32-3344 505-434-
9001 ),
%w(Tuco Salamanca 99 Crystal Springs Lane Albuquerque NM 575-44-3553 505-
776-0455 ),
%w(Saul Goodman 9800 Montgomery Blvd NE Albuquerque NM 585-19-9990 505-503
-4455 )
]
artists = [
# column names
%w( first_name last_name telephone address city state zip_code birthdate salary
),
[ 'Vinh',
'Tranh',
'438-910-7449',
'8235 Maple Street',
'Wilmington',
'VM',
'29085',
'9/23/63',
'1200'
],
[ 'William',
'Kopf',
'846-836-2837',
'6937 Ware Road',
'Milton',
'PA',
'93756',
'9/21/46',
'43500'
],
[ 'Yukio',
'Takeshida',
'387-827-1095',
'13 Uno Lane',
'Ashville',
'NC',
'23556',
'7/1/29',
'57000'
],
[ 'Zippy',
'Pinhead',
'834-823-8319',
'2356 Bizarro Ave.',
'Farmount',
'IL',
'84357',
'1/1/67',
'89500'
],
[ 'Andy',
'Warhol',
'212-321-7654',
'231 East 47th Street',
'New York City',
'NY',
'10017',
'8/6/1928',
'2700000'
]
]
all_arrays = [ escargot_player_data, employees, artists ]
all_arrays.each do |array| # Accessing 2-D Array from all_arrays
for a in array # Accessing Each array from 2-D Array
puts "*******ROW*******"
for ad in a # Accessing Each Row from each Array
puts ad # Printing Each Column from Each Row from Each Array from 2-D Array
end
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.