On Mon, Jan 4, 2010 at 6:20 AM, Robert Klemme
<shortcutter / googlemail.com> wrote:

> The first version invokes method Regexp#=~ and the second version invokes
> String#=~ - which happen to do roughly the same although I believe the
> second one to be a tad slower.      > of the speed difference and regular expression matching is rather an
> operation of Regexp than of String.

Very technically speaking, calling the Regexp#=~ will be
microscopically faster because it avoids a tiny bit of C code that
gets executed when calling the String#=~ version. In practice though,
the difference is so small as to be invisible against the noise of the
rest of the system.

String#=~ just has a little sugar built in.

If it's passed a Regexp, then it just calls the C function underlying Regexp#=~.
If it is passed a String, it complains.
If it is passed anything else, it tries to call #=~ on what it was
passed, passing itself as the argument.  This lets one build custom
matching classes that Strings can use with the =~ syntax.


Kirk Haines