1

I have two arrays

names = ["name1", "name2"]
tracks = ["track1", "track2"]

I'm trying to combine the two arrays into one new array

data => ["name1 track1", "name2 track2"]

I tried the following

1)   @data = @name.zip(@tracks)flatten 
     produces ["name1", "track1", "name2", "track2"]

2)   @data = @name.at(0).concat(@tracks.at(0)) + @name.at(1).concat(@tracks.at(1))
     produces ["name1track1name2track2"]

3)   @name.each do |n|
      @tracks.each do |t|
       @data.push n + " " + t
       end
     end
     produces ["name1 track1", "name1 track2", "name2 track1", "name2 track2"]

I cannot seem to solve this problem. I would appreciate some help.

1
  • Those new to SO commonly select an answer too soon (and thereby discourage other answers) or never select an answer. If at least one answer meets your needs--as here (considering your comment on @steenslag's answer)--you should select the one you like best. There's no rush, just don't forget to do it. Have a look at this FAQ. Commented Feb 20, 2014 at 19:00

8 Answers 8

6
names  = ["name1", "name2"] #note the "="
tracks = ["track1", "track2"]

p names.zip(tracks).map{|e| e.join(" ")}
#=> ["name1 track1", "name2 track2"]
Sign up to request clarification or add additional context in comments.

1 Comment

I went with this answer and it has worked perfectly. Thank you so much!
1
names.map {|name| "#{name} #{tracks[names.index(name)]}"}
=> ["name1 track1", "name2 track2"]

1 Comment

If names => ['cat', 'cat'] there's a problem.
1

You can do it like this:

data = []
names.each_with_index {|x,y| data << "#{x} #{tracks[y]}" }
data # => ["name1 track1", "name2 track2"]

Comments

0

Here's a neat way to do it:

names = ["name1", "name2"]
tracks = ["track1", "track2"]

names.zip(tracks).map { |a| a.join(" ") }
# => ["name1 track1", "name2 track2"]

First,

names.zip(tracks)

gives us

[["name1", "track1"], ["name2", "track2"]]

And then we join the inner arrays with a space using map

Comments

0

I like the product method

=> array_size = names.size
=> names.product(tracks).
     each_with_index.
     map { |x,i| x.join(" ") if (i % array_size - i / array_size).zero? }.compact
=> ["name1 track1", "name2 track2"]

2 Comments

If array.size => 1,000, you'd create an array with one million elements, then throw away 999,000 of them!
yeap, didn't say it was, fast or cheap. :-)
0

Another:

names.map { |s| s << ' ' << tracks.shift }
  #=> ["name1 track1", "name2 track2"]

If names (and/or tracks) cannot be modified, this would have to operate on a copy.

A variant:

[].tap { |a| names.size.times {a << (names.shift + ' ' + tracks.shift) } }

Comments

0

A bit different way :

names  = ["name1", "name2"] 
tracks = ["track1", "track2"]

names.each_index.map { |i| "#{names[i]} #{tracks[i]}" }
# => ["name1 track1", "name2 track2"]

Comments

0

Another way, very similar to @steenslag's (I sometimes hate opening blocks :-) )

names = ["name1", "name2", "name3"]
tracks = ["track1", "track2", "track3"]

names.zip([' '] * names.length, tracks).map(&:join)
=> ["name1 track1", "name2 track2", "name3 track3"] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.