On Nov 8, 2005, at 11:52 AM, aurelianito wrote:
> Hi all!
>
> I've been trying to optimize the code
> a_string.gsub(/pattern_1/, "REPLACE_1").gsub(/pattern_2/,
> "REPLACE_2").gsub(/PATTERN_3/,"REPLACE_3").
So you've profiled your code and determined that gsub is your slow
point?
If so, you've also checked that regex matching is not slowing you
down, but the creation of a new string and the garbage collection of
the old string is?
> IMHO, this two blocks of pseudocode should behave in the same way, but
> if the second pattern don't matches, it returns null on the second
> version, and then generates an exception.
> Why is it than the destructive gsub behaves differently?
$ ri String#gsub!
----------------------------------------------------------- String#gsub!
str.gsub!(pattern, replacement) => str or nil
str.gsub!(pattern) {|match| block } => str or nil
------------------------------------------------------------------------
Performs the substitutions of +String#gsub+ in place, returning
_str_, or +nil+ if no substitutions were performed.
> Do you think that the current behaviour is the right behaviour? Why?
Yes.
Bang methods can change things in places you don't expect.
Bang methods should give an indication that something was changed if
something was changed.
--
Eric Hodel -
-
http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04