I was trying to use gsub
to remove non word characters in a string in a rails app. I used the following code:
somestring.gsub(/[\W]/i, '') #=> ""
but it is actually incorrect, it will remove letter k
as well. The correct one should be:
somestring.gsub(/\W/i, '') #=> "kkk"
But my problem is that the unit test of a rails controller which contains the above code using rspec does not work, the unit test actually passes. So I created a pretty extreme test case in rspec
it "test this gsub" do
'kkk'.gsub(/[\W]/i, '').should == 'kkk'
end
the above test case should fail, but it actually passes. What is the problem here? Why would the test pass?