Q:

When to use Kernel#gsub in Ruby?

I mostly use String#gsub . I am familiar with it also. There is another Kernel#gsub which I have never used.
I tried to explore this as below :
(arup~>~)$ echo 'matz' | ruby -n -e 'puts $_.gsub(/./,"*")'
****
(arup~>~)$ echo 'matz' | ruby -n -e '$_.gsub(/./,"*") ; puts $_'
matz
(arup~>~)$ echo 'matz' | ruby -np -e '$_.gsub(/./,"*")'
matz

Docs is saying - Equivalent to $_.gsub, except that $_ will be updated if substitution occurs. But in my case, it seems $_ has not been updated.
The reason is String#gsub has been called. Not Kernel#gsub. See below :
(arup~>~)$ echo 'matz' | ruby -n -e 'p $_.method(:gsub)'
#<Method: String#gsub>

How to call Kernel#gsub ?
Why we need this method, while we have String#gsub ?

The same question I asked in Ruby Forum, but didn't get any answer.

A:

Using the same examples you used above this should illustrate the point:
Using Kernel.gsub
echo 'matz' | ruby -np -e 'puts "before: " + $_; Kernel.gsub(/./,"*"); puts "after: " + $_'
# -> before: matz
# -> after: ****

and using $_.gsub
echo 'matz' | ruby -np -e 'puts "before: " + $_; $_.gsub(/./,"*"); puts "after: " + $_'
# -> before: matz
# -> after: matz

As you can See $_ is only updated when using Kernel.gsub
edit:
Some additional info, sprinkled with a little assumption on my end
Taken from ruby source the descriptive alias for $_ is $LAST_READ_LINE. This is what the inline comments where the alias is created say:

The last line read by Kernel.gets or
    Kernel.readline. Many string-related functions in the
    +Kernel+ module operate on $_ by default. The variable is 
    local to the current scope. Thread local.

Now according to the c code for what ends up being made available as Kernel#gsub (defined in string.c)
static VALUE
rb_f_gsub(int argc, VALUE *argv)
{
  VALUE str = rb_funcall_passing_block(uscore_get(), rb_intern("gsub"), argc, argv);
  rb_lastline_set(str);
  return str;
}

This code uses the internal gsub method on a string and then explicity assigns $_ to the new value before returning the mutated string itself.
The only reason I can come up with is that Kernel#gsub would have been used internally for some kind of pre-sanitization of input that would always occur. As a convenience method of sorts, immediately assigning the sanitized value to $_ for further use without having to explicitly assign and return $_.
I'm saying would have been because it turns out that the changelog for ruby 1.9.1 lists Kernel#gsub as deprecated

Deprecation

o Kernel#getc, #gsub, #sub

The usage for Kernel#gsub in c is rb_intern("gsub") and I can't actually find any reference to it being used at all in the source of

1.8.7p371
1.8.0
1.7.1
1.6.0
1.3.1
1.0.0

I must be missing something here, the source for rb_f_gsub changed and was actually moved from string.c in 1.8.7 to ruby.c in 1.9.3, somewhere in between.
With all this, I think, we can answer the original question of why we need it - I guess we don't, not anymore at least.
So the question now becomes, when was it ever actually used and how?

