문제

I have a String array which I want to convert to specifc format. For example

y = ["hello","how","you"]

And the output should be the following exact String

[["hello","hello"],["how","how"],["you","you"]]

I have currently used the following way which is working fine for me, but I need to know is there a better way to do this in Ruby

"[#{y.collect {|x| "[#{["\"#{x}\"", "\"#{x}\""].join(",")}]" }.join(",").to_s}]"
도움이 되었습니까?

해결책

You can use zip for this:

y.zip(y).inspect

EDIT - just noticed you were after a string - I think inspect should do the job.

다른 팁

s = y.zip(y).inspect
puts s
#=> [["hello","hello"],["how","how"],["you","you"]]    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top