2

I have following sveral arrays which each of them consists into a String.

x = ["t", "o", "d", "a", "y"]
y = ["i", "s"] 
z = ["s", "u", "n", "d", "a", "y"]

my output should be like following:

x = [today]
y = [is] 
Z = [sunday]

in together: today is sunday

How can i get expected array using ruby?

1
  • What is the today in [today] – a string? Then it should be ["today"]. Commented Nov 14, 2018 at 8:40

3 Answers 3

4

You will want to use the #join(separator) method.

See the official ruby docs for Array#join

Example:

['h', 'e', 'l', 'l', 'o'].join('')
=> "hello"

A good place to start learning the basics of Ruby is at Code Academy.

I also recommend dash for browsing documentation offline!

Sign up to request clarification or add additional context in comments.

2 Comments

You can just call join, the empty string argument isn't needed.
@Stefan is right and it should be called without an argument when an array is being joined without a separator
2

For final your output,

[x, y, z].map(&:join).join(' ')

Comments

0

You can use the .join() method like this:

x = ["t", "o", "d", "a", "y"]
y = ["i", "s"] 
z = ["s", "u", "n", "d", "a", "y"]

x.join()
=> "today"
y.join()
=> "is"
z.join()
=> "sunday"

Then do this:

x.join + y.join + z.join()
=> "todayissunday"

Or combine x, y, z into one array and call join on it, like this:

Array(x + y + z).join
=> "todayissunday"

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.