0

I have the bellow [sic] two arrays:

first_name = ["prabhu" ,"raghu" , "satish"]
second_name = ["chaitanya", "varma", "venkey"]

I want to print the following output:

full_name = ["prabhu chaitanya","raghu varma", "satish venkey"]
1
  • If you can modify second_name, you could do this (but using zip is the obviously what you should be using): first_name.map { |f| "#{f} #{second_name.shift}" }. Commented Apr 28, 2015 at 7:14

4 Answers 4

4

You can try the following:

first_name.zip(second_name).map{ |x| x.join(' ')}
Sign up to request clarification or add additional context in comments.

Comments

3
[first_name, second_name].transpose.map{|a| a.join(" ")}

1 Comment

I like this method. I tried with zip method not know with transonse can be possible. Sawa you are great. Your answers are always unique and let me learn new trick everytime. I m big fan of u ;)
2

You can try this also:

full_name = [];
first_name.each_with_index {|x, i|  x + second_name[i]}

Comments

0
first_name.zip(second_name).map { |f, l| "#{f} #{l}" }
=> ["prabhu chaitanya", "raghu varma", "satish venkey"]

Alternatively:

first_name.zip([" "].cycle, second_name).map(&:join)
=> ["prabhu chaitanya", "raghu varma", "satish venkey"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.