How come I can do
ruby -pe "sub /.*{((\d+\.){3}).*/, '\115'" < file.txt
but if I try scan instead of sub I get
-e:1:in
<main>': undefined methodscan' for main:Object (NoMethodError)
This is confusing since sub isn't a method on Object either.
I also tried it the Perl way, something like
ruby -ne "/.*/; puts $1" <file.txt
but that didn't fly.
Where can I find the documentation covering this?
sub is a method on Kernel, a module whose instance methods are globally available. This version of the method operates on the global variable $_ which contains the string last read by gets.
This is a totally different method than String#sub, which does a similar process but with an explicit string as the receiver, rather than implicit use of $_.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With