Forum: Ruby Why we need Kernel#gsub ?

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-03-19 13:39
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 ?
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (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 $_'
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` ?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.