Question on the .gsub method


#1

Hi,

This is my first post as a trial in the Code Academy courses.

I was trying to figure out how to use the .gsub method for removing stuff like periods commas and that kind of stuff. I got it to kind work using this;

words = text.to_s.gsub(/[^a-z0-9\s]/, ’ ')

but I noticed it was also removing capital letters so I tried to search around. The ruby /ruby-doc.org was not very helpful and I was wondering if someone new a solid resource for ruby.

Anyways any insight on how I can keep the capital letters while only removing the punctuation.

Thanks in advance.

Nick

Looks like I found a way around it with

words = text.downcase

but if theres an easier way to do this in the .gsub method I’d love to know.


#3

At the beginner end of the learning spectrum, ‘easier’ is an oxymoron, since regular expressions are at the other end of the scale. If anything we should be thinking up algorithms for parsing text with conventional methods, and eventually Ruby built-ins. But that’s just a rant, on my part.

text = ['"A"3.14,', 'b"6.7".', '"C9";']
puts text.to_s.gsub(/[^\w\d\s]/i, '')
# A314 b67 C9

We’ve defined a class that allows anything that is NOT a word character, nor a digit, nor whitespace and given our pattern a case insensitive flag. Anything matching this pattern in our text will be replaced with an empty string.

That aside, do turn one’s attention to solving this without .gsub. Skipping that is like robbing one’s self of one’s childhood.


#4

Thanks Roy. This was very helpful and not just coding wise. Appreciate the insight on the bigger picture.