Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics (Retired) Ruby Strings String Methods

Eric Cross
Eric Cross
1,623 Points

Purpose of reversing method

I'm not really sure what the purpose of reversing a string would be? I understand the need to uppercase, or lowercase it though.

2 Answers

On a string of characters I cannot think of a good reason to use reverse. That said, reverse is a method applies to many different things, strings only being one of them. For example, you can reverse the contents of an array.

foo = [1,2,3,4]
bar = foo.reverse

This could be useful for sorting, say that you want some output to be either ascending (1,2,3,4) or descending (4,3,2,1). That's just one example off the top of my head though!

Eric Cross
Eric Cross
1,623 Points

Nice. Thanks Joe, hadn't thought about reversing arrays.

Gavin Ralston
Gavin Ralston
28,770 Points

Palindrome testing is one very useful example of string.reverse