Forum: Ruby Why we need Kernel#gsub ?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5627b1da66c6ce007b5b5409dd2ed364?d=identicon&s=25 Arup R. (my-ruby)
on 2014-03-19 13:39
I mostly use String#gsub ( ) . I am
familiar with it also.

But today I found Kernel#gsub ( ), which I
have never used it.

What is the use-case of this method ?
5627b1da66c6ce007b5b5409dd2ed364?d=identicon&s=25 Arup R. (my-ruby)
on 2014-04-02 23:04
I tried to explore this as below :

(arup~>~)$ echo 'matz' | ruby -n -e 'puts $_.gsub(/./,"*")'
(arup~>~)$ echo 'matz' | ruby -n -e '$_.gsub(/./,"*") ; puts $_'
(arup~>~)$ echo 'matz' | ruby -np -e '$_.gsub(/./,"*")'

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 topic is locked and can not be replied to.