1

Like these python below

[m + n for m in 'ABC' for n in 'XYZ']
>>> ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']

How to archive it in ruby in one line?
thanks a lot.

2 Answers 2

2

Another one using Array#product and Array#join:

'ABC'.chars.product('XYZ'.chars).map(&:join)
#=> ["AX", "AY", "AZ", "BX", "BY", "BZ", "CX", "CY", "CZ"]
Sign up to request clarification or add additional context in comments.

1 Comment

nice one, less block.
1

In Ruby one way of doing this is:

'ABC'.chars.flat_map { |a| 'XYZ'.chars.map { |b| a + b } }
# => ["AX", "AY", "AZ", "BX", "BY", "BZ", "CX", "CY", "CZ"]

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.