hancho
hancho

Reputation: 1437

How does this iterative reverse method work in Ruby?

I have been building small programs for the last 2 months while also taking a few courses online to help me learn coding. It's been going well and I feel like the question I am about to ask is something simpler than some of the stuff I've written.

Below are two methods to reverse a string. I understand method 2, I do not understand how method 1 works. They both give the same output. Can anyone explain how the first method works?

1.

def reverse(string)
  reversed_string = ""

  i = 0
  while i < string.length
    reversed_string = string[i] + reversed_string

    i += 1
  end

  return reversed_string
end

2.

def rev(string)
  string.reverse
end

Upvotes: 1

Views: 149

Answers (3)

Topher Hunt
Topher Hunt

Reputation: 4813

I'd encourage you to open irb in your terminal and experiment with what effect each line of this method would have. You can read all the explanations in the world, but you'll only get a solid feel for how the language works by typing stuff in and seeing output on the screen.

A String is basically an array or a long list of characters, eg. ["H", "e", "l", "l", "o"]. Method #1 sets up a while loop that repeatedly takes an action once per each character in the string. More specifically, it takes the character at each position (string[i]) and adds it to the beginning of a "result" string (reversed_string) which gradually grows until it's as long as the original string was. Except each letter is added to the beginning of the result string, so if the original string is "Hello!", the result string will start with an "H", then will become "eH", then "leH", and so on.

Big-picture note: If you're learning to program for the first time, this is all well and good; Ruby is a fantastic language choice with a thriving and friendly community, and this style of for or while loop is a staple of most languages and are well worth learning and practicing. But you should be aware that the logic used in method #1 is not idiomatic Ruby; yes it will work fine, but I think most experienced Ruby developers would look at that code and say "Ick. Why didn't you just take the array and run .each on it?" Ruby is a very flexible language but certain code patterns have emerged as common or "favorite" practices, and in my experience, the structure where you use an integer to step through a while loop isn't one of them.

Keep that in mind, and once you get through your current learning materials, consider looking around to find a course that teaches you Ruby in "the Ruby way".

Upvotes: 2

Makoto
Makoto

Reputation: 106508

The first method is simply prepending each character of the original string to a new string, which results in the reversal of the string itself.

string[i] + reversed_string means:

  • Take the ith position of the string
  • Prepend it to reversed_string

Since reversed_string starts empty, this works conveniently well.

For a string "apples", that would result in:

  • 'a' + ''
  • 'p' + 'a'
  • 'p' + 'pa'

...and so forth.

Upvotes: 1

br3nt
br3nt

Reputation: 9606

Lets say string = 'Hello'

When i = 0, string[0] is referring to H and reversed_string is ``. Therefore, after reversed_string = string[i] + reversed_string, reversed_string is H.

i is then incremented.

When i = 1, string[1] is referring to e and reversed_string is H. Therefore, after reversed_string = string[i] + reversed_string, reversed_string is eH. e is concatenated to the front of the reversed_string string.

i is then incremented.

When i = 2, string[2] is referring to l and reversed_string is He. Therefore, after reversed_string = string[i] + reversed_string, reversed_string is leH. l is concatenated to the front of the reversed_string string.

i is then incremented.

The loop continues until i equals string.length, meaning there are no more letters to move to the front.

Upvotes: 0

Related Questions