3 ms·
You may be aware of this, but for those who aren't: Ruby has much of this. Perl is perhaps Ruby's other "parent" besides Smalltalk, for good or bad, and there a
by vidarh 9y ago
You may be aware of this, but for those who aren't: Ruby has much of this. Perl is perhaps Ruby's other "parent" besides Smalltalk, for good or bad, and there are a lot of stuff stolen from Perl that sees relatively little use in Ruby today.
The "-n" switch adds an implicit "while gets; ....; end" loop, leaving the input line in $_. "-a" will do "$F = $_.split", which means it splits on the contents of "$;" which can be set with "-F" like with awk.
E.g.:
ruby -naF[someseparator] -e '[some ruby expression]'
Is equivalent to wrapping [some ruby expression] in:
$; = [someseparator]
while gets
$F = $_.split($;)
end
Which incidentally also gives a halfway decent awk impersonation given that Ruby also supports awk-like BEGIN {} and END{}.
- rjeli 9y agoMore on this: https://robm.me.uk/ruby/2013/11/20/ruby-enp.html https://robm.me.uk/ruby/2013/11/20/ruby-enp.html https://robm.me.uk/ruby/2013/12/03/argf-ruby.html https://robm.me.uk/ruby/2013/12/03/argf-ruby.html https://robm.me.uk/ruby/2015/10/31/dollar-underscore.html https://robm.me.uk/ruby/2015/10/31/dollar-underscore.html and his book: https://pragprog.com/book/rmtpruby/text-processing-with-ruby https://pragprog.com/book/rmtpruby/text-processing-with-ruby