Why we need Kernel#gsub?

I mostly use String#gsub (
http://www.ruby-doc.org/core-2.1.0/String.html#method-i-gsub ) . I am
familiar with it also.

But today I found Kernel#gsub (
http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-gsub ), which I
have never used it.

What is the use-case of this method ?

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>

What am I missing in the doc ? How to call Kernel#gsub ?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs