简体   繁体   中英

Printing an array of arrays on one line in console (one line per master array object) in Ruby

I have an array of arrays that is currently printing each object in the array on its own line. The master array holds many different people inside of it. Each person has 5 different objects stored to them (eg Last Name, First Name, DOB.. etc)

Kournikova
Anna
F
6/3/1975
Red

Hingis
Martina
F
4/2/1979
Green

Seles
Monica
F
12/2/1973
Black

What I'm trying to do is print out each person and their corresponding objects on one line, per person.

Does anyone have a solution for this? Additionally, the output should not contain array brackets ( [] ) or commas. I'm wondering if it will simply need to be a string, or if there is something I am missing.

Some of my code below:

space_array = [split_space[0],split_space[1],split_space[3],new_date,split_space[5]]
master << space_array 
puts master

The ideal output would be something like this:

Kournikova Anna F 6/3/1975 Red
Hingis Martina F 4/2/1979 Green
Seles Monica F 12/2/1973 Black
your_array.each do |person|
  puts person.join(" ")
end

The method puts will automatically put a new line. Use print instead to print the text out with no new line.

Or if you want, you can use the join function.

['a', 'b', 'c'].join(' ') 
=> 'a b c'

You can just iterate over the outer array and join the inner arrays into a string. Since you provide no example data ready for copying and pasting, here's some example code I made up:

outer_array.each { |inner| puts inner.join(' ') }

更简单:

puts your_array.join(" ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM