Tscott
Tscott

Reputation: 485

How to break down a string that's in an array even further

I'm trying to break down a sentence that's part of one string, but break it down into letters in their own array and have that inside one big array.

So what I mean is:

def break("hello world")

the code in the method would than result in this:

[["h","e","l","l","o], ["w","o","r","l","d"]]

The reason why I need it like that is so I can rearrange the letters in the order I want later. I've tried several things, but no luck.

Upvotes: 1

Views: 108

Answers (2)

Santhosh
Santhosh

Reputation: 29124

"hello world".split.map &:chars
# => [["h", "e", "l", "l", "o"], ["w", "o", "r", "l", "d"]]

Upvotes: 5

seph
seph

Reputation: 6076

I wouldn't use break as a method name. It's a key word in the language.

def break_it(str)
  str.split.map { |word| word.each_char.to_a }
end

break_it("hello world")

Upvotes: 2

Related Questions