0

So here is the string I want to convert to an array, where then I want to reverse each word without reversing the entire sentence, and then join them back and provide the output.

For instance, I want to change "Hello there, and how are you?" to "olleH ,ereht dna woh era ?uoy"

This is the string:

sentence1="Hello there, and how are you?"

and, this is my code in which I have to incorporate .each(which i know is wrong, but don't know how)

def reverse_each_word(sentence1)
  split_array = sentence1.split
  reversed_array = split_array.reverse
  reversed_array.each do |joined_array|
    joined_array.join(' ')
  end
end

and as mentioned, the desired result has to be:

"olleH ,ereht dna woh era ?uoy"
1
  • Why do you know it’s wrong? (besides not getting the output you desire) Can you explain the purpose of each line of code [to yourself] and identify what isn’t working as you expect [to us]? This is a very important exercise in producing a minimal reproducible example and an acceptable question for StackOverflow. Commented Aug 24, 2019 at 21:58

2 Answers 2

3

You're calling join in a string, since you're iterating over each element in reversed_array, and all those ones are string objects:

p sentence1.split.first.join(' ')
# undefined method `join' for "Hello":String (NoMethodError)

It might work if you use something to store the value in each iteration within the block, it can be a variable declared outside the iteration, or better map, after that, you can just reverse each string and then join everything:

def reverse_each_word(sentence1)
  sentence1.split.map do |joined_array|
    joined_array.reverse
  end.join(' ')
end

p reverse_each_word(sentence1) # "olleH ,ereht dna woh era ?uoy"

Notice this can be written as sentence1.split.map(&:reverse).join(' ') too.


In case you're looking for each to solve this problem, you'll need a variable where to store each "modified" string as long as you're iterating over each of those elements:

memo = ''
sentence1.split.each { |joined_array| memo << "#{joined_array.reverse} " }
p memo.rstrip # "olleH ,ereht dna woh era ?uoy"

There you have a memo variable which is an empty string, just for the reason to be filled with each reversed string, you reverse the string and add a white space to the right. The last string is going to have an additional whitespace, so rstrip helps you to "remove" it.

For collect you can use the map approach, because they're aliases.

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

4 Comments

Thank you so much for your help! I'm starting to learn about all of these and sometimes it's just too hard to grasp things right away. So, we add the '.join' after the 'end' when we 'do/end', or can the join be used inside of the do/end scope?
There's never going to be a rule for this, as it depends on your case, here you need to join all elements within the array, so for that, it must be done after the block (do/end).
Perfect! another thing that I forgot to add is that this homework question asked me to implement '.each' and '.collect' to get the same results. Does your solution implement the .each? if not, can you guide me how to solve it using .each and .collect? Thank you very much
Ah! sorry, I just gave you an improvement instead of applying each. See the updated answer.
2

I would be inclined to use String#gsub with a regular expression.

str = "Hello there, and how are you?"

str.gsub(/\S+/) { |s| s.reverse }
  #=> "olleH ,ereht dna woh era ?uoy"

The regular expression reads, "match one or more characters other than whitespace characters".

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.