I mostly use String#gsub (
Class: String (Ruby 2.1.0) ) . I am
familiar with it also.
But today I found Kernel#gsub (
Module: Kernel (Ruby 2.1.1) ), which I
have never used it.
What is the use-case of this method ?
I mostly use String#gsub (
Class: String (Ruby 2.1.0) ) . I am
familiar with it also.
But today I found Kernel#gsub (
Module: Kernel (Ruby 2.1.1) ), 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.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs