Reverse the Letters in a String Without Using the Reverse Method in Ruby

In this coding exercise you’ll re-create the reverse method in Ruby. You are required to build a method that reverses the characters in a string and returns the reversed values.

Summary

Build a method that reverses the characters in a string without using the reverse method.

Exercise File

Code File

Exercise Description

You will need to perform monkey patching and open up the String class in Ruby. From there create a method called alt_reversethat reverses the characters in a string without calling the reverse method provided by Ruby.

Sample Use

"Hi there".alt_reverse # "ereht iH"

Real World Usage

In a real life program you would call the reverse method directly, so this method isn’t necessary. However in this exercise you will:

  • Learn three important operations that can be performed on the String class in Ruby.
  • Implement monkey patching

Additionally, this is a common Ruby coding interview question that tests an applicant’s knowledge of the String class.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on