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
Andrew Stelmach
12,583 PointsExtra Credit: How to properly count number of characters in a string?
#make a program that asks what your name is. Have the program print out the number of characters in your name. Also have the program print out a message if your name is longer than 25 characters.
#1. ask name
puts "What is your name?"
#assign to variable 'name'
name=gets.gsub!(/\s+/, "")
#3. Print number of characters in name
puts "Your name is #{name.length} characters long."
#4. If name.length is > 25, print a message.
if name.length > 25
puts "That is SOOO fking long!"
end
This works, BUT I don't understand why gsub!(/\s+/, "") works. If you put in "Andrew Stelmach" when you ask for you name, it returns 14, which is correct. I tried using .strip but that returns 15, because it only removes whitespace from the beginning and end of a string. .squish doesn't work either.
Can someone explain what gsub!(/\s+/, "") is, exactly how it works, and if it's overkill? I'm guessing there might be a more elegant and concise solution.
5 Answers
John Steer-Fowler
Courses Plus Student 11,734 PointsHi Andrew,
I'm struggling a little bit to understand exactly what you want from an answer here. However, I will answer what I know.
gsub is a Ruby method that can be used to substitute characters in a string. In this case, gsub!(/\s+/, "") is being used to take out the whitespace in the string being provided by the user through gets (get string). The regular expression here is to find \s (whitespace) and replace it with "" (blank).
Hope this helps in some way. I may actually be completely wrong, but this is how I gathered the gsub method to work :D
notf0und
11,940 Points.gsub() is a method that will find a specified pattern, and replace it with whatever it is you specify. In this case, you've told .gsub() to find all whitespace characters, and replace them with no space. As I understand it, the \s+ in .gsub(/\s+/, '') will find all 1+ whitespace characters (spaces, new line, tab).
As for a more elegant way to count the total number of letters in someone's full name, I think .gsub() might be your best bet, since you'd probably want to add dashes and maybe apostrophes to your pattern.
Hope that helps!
Andrew Stelmach
12,583 PointsWhat's the syntax for that, Bryan? ie searching for dashes, apostrophes AND +1 whitespace?
Or, if you like, what would I need to search for on google? :-)
notf0und
11,940 Pointshttp://www.ruby-doc.org/core-2.1.2/String.html find gsub on that page for exact information. I'm not sure I'm the best person to explain, but it's my understanding that .gsub(/[\s+-']/, '') or something similar would replace the necessary characters. I'll confirm next time I'm at my computer :)
notf0und
11,940 PointsAlright, I tested out (Ruby 2.1.2), and it seems the + character isn't required at all. \s will get rid of all whitespace, though I imagine things such as \n will only remove new line whitespace, or however many spaces equals that.
You do want to be careful with the + and - characters though (and I imagine some others). If you did this: name.gsub(/[\s+-']/, ''), Ruby would give you an error about an empty range, because the + and - are directly beside each other. You could fix it like so: name.gsub(/[\s+'-]/, '') and Ruby would treat the - properly as a dash. Like I said though, it seems that the + character is not needed, so I would place it away from any \s, \t, or \n's, or any similar things.
In summary, the syntax to search for dashes, apostrophes, and +1 whitespace is as follows: name.gsub(/[\s'-]/, ''). Hope that makes sense and is helpful!
John Steer-Fowler
Courses Plus Student 11,734 PointsHi Bryan,
The + sign is usually required if you want the Regex to match 1 or more times.
Does it still do this if you remove the + sign?
notf0und
11,940 PointsIn my tests without the +, all regular spaces and all \n characters were removed. I'm not sure if it's exactly safe, but it's definitely not required.
Andrew Stelmach
12,583 PointsOk I've been looking at some documentation, and gsub!() is the method being called and I THINK the two arguments inside are: 1. what to look for (this goes between the two "/" characters), and 2. what to replace that with (goes between the two " characters).
So I guess \s+ means whitespace.
John Steer-Fowler
Courses Plus Student 11,734 PointsI explained all of that in my first reply. But yes you have the right idea now.
Keep up the good work
John Steer-Fowler
Courses Plus Student 11,734 PointsIf you go here: Rubular this website helps you test Ruby regular expressions and even provides you with some of the most commonly used regular expressions toward the bottom.
You learn more about Regex or Regular Expressions as you progress further with the Ruby language.
Andrew Stelmach
12,583 PointsAnd, by the way guys, what's the best Ruby documentation site? There seems to be several ones all competing for top spot.
John Steer-Fowler
Courses Plus Student 11,734 PointsI just use http://ruby-doc.org/
But I purchased an awesome Ruby book that had a great documentation bit in the back of the book. Whenever I find something on Treehouse or elsewhere I look it up in the back of the book.
Andrew Stelmach
12,583 PointsGoing to sleep now, I'll pick up replies on Sunday. Thanks!
Andrew Stelmach
12,583 PointsAndrew Stelmach
12,583 PointsAndrew Stelmach is 14 characters long, not 15 (when you don't include the whitespace) - that's what I want the program to return.
Thanks for your answer. It answers it to a certain degree, but I'm interested in what the /\s+/, "" means. Is it all necessary, or just part of it?
John Steer-Fowler
Courses Plus Student 11,734 PointsJohn Steer-Fowler
Courses Plus Student 11,734 PointsHey Andrew,
Sorry I didn't make it that clear.
The /\s+/,"" is the regular expression. This is what tells gsub what to substitute in the string if that makes sense.
You might need to do some research on regular expressions in Ruby. I am not at the level to explain much about them, all I know is that they tell methods "where to look" in the string.
Maybe Jason Seifer can help you more than I can. I might be getting it all wrong.