Ruby command line with one layer against scanning

Why can i do

ruby -pe "sub /.*{((\d+\.){3}).*/, '\115'" < file.txt 

but if I try scan instead of sub , I get

-e: 1: in <main>': undefined method scan' for main: Object (NoMethodError)

This is confusing since sub not a method on Object .

I also tried using Perl, something like

 ruby -ne "/.*/; puts $1" <file.txt 

but it did not fly.

Where can I find documentation covering this?

+6
source share
1 answer

sub is a method on Kernel , a module whose instance methods are available worldwide. This version of the method works with the global variable $_ , which contains the line the last gets .

This is a completely different method than String#sub , which performs a similar process, but with an explicit string as the recipient, rather than implying the use of $_ .

+9
source

Source: https://habr.com/ru/post/954980/


All Articles