Why it does not work when you use punctuation?


i just want to ask about exercise: "Create a Histogram"


it looks like the code is correct, and it is, but when I type: "i am stupid, very stupid" the count of word stupid is ["stupid", 1], ["stupid,",1] and i get that it is that waz because of that comma, so I just want to ask, if there is someway to fix it


This is rather crude, but it removes all non-word characters, including numbers.

words.each { |word| word.gsub(/[A-Za-z]/,'') }

Insert this line immediately after words = text.split(" ").


This is wrong! You are actually replacing everything except non-word characters with ' ' .
however, we could do this instead:
words = text.scan(/[A-Za-z]+/)

here we have used regular expressions which is merely covered in the course. You can learn about it by simply searching ruby+regular expressions in your favorite search engine.


When we're wrong, it gets noticed. When we're right, it gets copied. Better the former. That way the learner has to do some look-up, as you have suggested.

It does come up in the Thith Meanth War unit, and the member asked for how to strip punctuation. My reply was meant to steer them in the right direction (even if it was wrong).


Yes I definitely see your point. But advising a learner to go about searching for a topic is better than providing wrong answer..I think.

And thank you, I edited my post accordingly about the coverage of regex.


Remember, the unit doesn't ask for it, so this is not solution code, intentionally. The user wants to learn something new, and this is their opportunity to do it of their own initiative.

But, rather than be cruel, here is an expression that does work...

words.each { |word| word.gsub!(/[^a-zA-Z]/, '') }

Text please: 
 this, is a, test.
test 1
a 1
is 1
this 1
[["test", 1], ["a", 1], ["is", 1], ["this", 1]]

Or even more succinctly:

words.each { |word| word.gsub!(/[^\w]/, '') }