6

Let's say that you have a string "Hello" and you want an array of chars in return ["H", "e", "l", "l", "o"].

Although it's a simple question I couldn't find a direct answer.

1 Answer 1

11

There are several ways to get an array out of a String. #chars which is a shortcut for thestring.each_char.to_a is the most direct in my opinion

>> "Hello".chars
=> ["H", "e", "l", "l", "o"]

The are other ways to get the same result like "Hello".split(//) but they are less intention-revealing.

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

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.