2

I want to perform deeper array method on a given string. Let's say I have str = "hello world"

I can split it using str.split(" "). But my intended goal is to turn "hello" into ["h", "e", "l", "l", "o"] and "world" into ["w", "o", "r", "l", "d"] so I can do array method of my choosing (like swapping first and last letter ["o", "e", "l", "l", "h"], and then returning it back into ["oellh", "dorlw"], and finally putting them back together "oellh dorlw".

That's just an example method. There are several different array method I wish to apply, but it only works if the string is split into array of words, and further split into array of elements (str.chars is not what I am looking for). I tried str.split(" ").each {|arr| arr.split("")} but it does not work. What is the best method to split a string into an array, and splitting that array of words into array of elements, apply a method, and join them back into a string?

1
  • use join to join an array of strings. If you don't want to use chars then maybe use str.split('') but this includes the space as well. Commented Jul 5, 2016 at 17:37

4 Answers 4

3
"hello world".split.map { |word| word[-1] + word[1..-2] + word[0] }.join(' ')
=> "oellh dorlw"
Sign up to request clarification or add additional context in comments.

1 Comment

Very simple and elegant solution.
2

You almost have your own answer, you are just slightly off:

str = "hello world"
=> "hello world"
str.split(" ").map { |s| s.split("") }
=> [["h", "e", "l", "l", "o"], ["w", "o", "r", "l", "d"]]

Use map instead of each.

The reverse:

a = str.split(" ").map { |s| s.split("") }
=> [["h", "e", "l", "l", "o"], ["w", "o", "r", "l", "d"]]
a.map { |s| s.join }.join(" ")
=> "hello world"

Comments

2

This is a really simple thing to do:

'hello world'.split.map{ |s|
  s[0], s[-1] = s[-1], s[0]
  s
} # => ["oellh", "dorlw"]
.join(' ') # => "oellh dorlw"

You don't need to create intermediate arrays of the word characters, which is a waste of CPU. Instead, s[0], s[-1] = s[-1], s[0] is using parallel assignment to swap the first and last characters.

Once you have the array of reversed words then join(' ') will return you to a string again.

You could use [...] * ' ' to accomplish the same thing but it's not as idiomatic:

["oellh", "dorlw"] * ' ' # => "oellh dorlw"

How that works is left for you to figure out.

If you want to convert a string into its characters, you can use split(''). chars is available and will do the same thing:

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

chars should be a tiny bit faster because Ruby isn't having to figure out what parameter was passed to split.

You can fold, spindle or mutilate all you want at that point.

I'd recommend reading through the Ruby class documentation a couple times. It'll help make you aware of what methods are available, so you can quickly search for these things.

Comments

1

To produce ["oellh", "dorlw"] you can write

str.split(" ").map{|arr| arr.reverse}

To produce an array of arrays like

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

you can do

str.split(" ").map{|arr| arr.split("")}

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.