Question

I have the below code that I need help with fixing this Ruby code. I am...

I have the below code that I need help with fixing this Ruby code. I am trying to iterate through these three arrays and print the array out with a label. The animals array should have the Animals header, the people should have the People header and the friends array should have the Friends header. I would also like to adjust the printout size based on the size of the elements in the array. I would like for it to look like the below.
%%%%%%%%%%%%%% Animals %%%%%%%%%%%%%%
Hector     Chihuahua       7
%%%%%%%%%%%%%% People %%%%%%%%%%%%%%
Jesse James     123 Homewood Drive     San Francisco      CA      510-555-1234     510-123-4567              

# A array containing animal data
animals = [
  # Column names: name, character, and points
  ['Hector','Chihuahua',7]
]

people = [
    #  Name Address City State SSN Telephone
    %w(Jesse\ James 123\ Homewood\ Home\ Drive San Francisco CA 510-555-1234 510-123-4567 )
]

friends = [
   # friends array
   %w( first_name last_name telephone address city state zip_code birthdate salary ),
   [ 'Jessica',
     'Simpson',
     '485-123-456',
     '9210 Cheery Avenue',
     'Tyler',
     'TX',
     '12345',
     '7/6/81',
     '500'
   ]
]

joined_arrays = [animals, people, friends]
array_titles = ["Animals", "People", "Friends"]
# Process each element
 
joined_arrays.each do |elem|
    elem.each {|el| puts el.join(" ")} 
end

i = 0 
while i < array_titles.length  
  puts "%" * 40 + array_titles[i] + "%" * 40
  i+=1
end
0 0
Add a comment Improve this question Transcribed image text
Answer #1
# A array containing animal data
animals = [
  # Column names: name, character, and points
  ['Hector','Chihuahua',7]
]

people = [
    #  Name Address City State SSN Telephone
    %w(Jesse\ James 123\ Homewood\ Home\ Drive San Francisco CA 510-555-1234 510-123-4567 )
]

friends = [
   # friends array
#    %w( first_name last_name telephone address city state zip_code birthdate salary ),
   [ 'Jessica',
     'Simpson',
     '485-123-456',
     '9210 Cheery Avenue',
     'Tyler',
     'TX',
     '12345',
     '7/6/81',
     '500'
   ]
]

joined_arrays = [animals, people, friends]
array_titles = ["Animals", "People", "Friends"]

i = 0 
while i < array_titles.length  
  # getting array of max len
  
  puts "%" * 40+ array_titles[i] + "%" * 40
  max_len = joined_arrays[i].max_by(&:length)
  # gtting the longest string
  len_m = max_len.map(&:to_s).max_by(&:length).length.to_i
  # iterating the same position array with help if index
  # muiltiplying spcae by longest element in join function
  joined_arrays[i].each {|el| puts el.join(" "*len_m)} 
  i+=1
  puts "\n" #remove this line if new line not required
end
#OUTPUT

Please do let me know if u have any concern...

6968663669696969696369696969696363696969696969696969696969636969%96969696%Animals686696969696969696969696969696969696696969696969669696969696966666 Hector Chihuahua 7 XXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXX XXXXX0 ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ Jesse James 123 Honewood Hone Drive San Francisco CA 510-555-1234 510-123-4567 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX F XXXXwvwXXXXXXXX XXXXXXXXXXXXX w Jessica simpson 485-123-456 9210 Cheery Avenue Tyler TX 12345 7/6/81 500 %

6968663669696969696369696969696363696969696969696969696969636969%96969696%Animals686696969696969696969696969696969696696969696969669696969696966666 Hector Chihuahua 7 XXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXX XXXXX0 ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ Jesse James 123 Honewood Hone Drive San Francisco CA 510-555-1234 510-123-4567 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX F XXXXwvwXXXXXXXX XXXXXXXXXXXXX w Jessica simpson 485-123-456 9210 Cheery Avenue Tyler TX 12345 7/6/81 500 %

Add a comment
Know the answer?
Add Answer to:
I have the below code that I need help with fixing this Ruby code. I am...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT