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
Alex Stophel
iOS Development Techdegree Student 11,552 PointsRuby Foundations: Strings Extra Credit -- Is this the best way to do it?
Write a Ruby program that asks you for a string. Have the program remove any punctuation from the string and then print it out in reverse.
puts "Please enter a string:"
string_in = gets.chomp
string_out = []
string_in = string_in.scan(/\w/) { |letter| string_out << letter }
puts string_out.join("").reverse
2 Answers
Daniel Reedy
25,284 PointsYou can simplify your answer by using String#gsub.
puts "Please enter a string:"
user_string = gets.chomp
user_string.gsub!(/\W/,'') # \W matches any non-word character [^a-zA-Z0-9_]
puts user_string.reverse
Josh Arnold
6,639 PointsWhy the / before and after the /W?
Daniel Reedy
25,284 PointsThey are the delimiters for Regular Expression strings in Ruby. Take a look at http://en.wikipedia.org/wiki/Regular_expression#Delimiters for more information.
Alex Stophel
iOS Development Techdegree Student 11,552 PointsAlex Stophel
iOS Development Techdegree Student 11,552 PointsThanks!