small medium large xlarge

23 Jul 2015, 14:31
Tim Chambers (2 posts)

You wrote:

Ruby has a bunch of “bang!” functions for in-place modification. Those are gsub!, capitalize!, downcase!, upcase!, delete!, reverse!, slice!, and others. It’s always a good idea to use them as much as you can.

Most of the time they CANNOT be used in my code because they do not return the unmodified string if unmodified - they return NIL.

So for instance:

would seem to be an ideal place to use bang variants (case insensitive match) but I risk it being already lower case. What am I missing.

31 Aug 2015, 21:03
Alexander Dymo (9 posts)

Tim, you are right. Their usefulness is limited, that’s why I wrote “as much as you can”. In your example you most likely don’t want to modify existing data during the comparison, so there’s no way bang functions can help you here.

29 Dec 2015, 13:04
Radoslaw Bulat (4 posts)

I don’t think there is any limitation related to this behaviour. You could rewrite your example like this:


Of course any time you use method which modify object you have to ask yourself “Am I owner of this object and can safely modify it?”.

You must be logged in to comment