110 points · by kawera · 7y ago · 48 comments
The command line switches are the same, the BEGIN/END block stuff is the same, the $F auto-splitting is the same.
Good on Ruby, that's the stuff that makes Perl one-liners actually worth coming back to again and again.
I use Ruby for text processing now, because it's easier to read than Perl, but I loop on stdin with IO.readlines, each and split. It could be less efficient but I'm not parsing billions of records. I value readability more than one liners.
That's fair. That kind of Perl is also very different than the kind of Perl you would use to write Mojolicious applications, or system utils, or daemons. Those features are very specifically for quick one-liners[1], and in some cases don't even make sense outside of that.
> I use Ruby for text processing now, because it's easier to read than Perl, but I loop on stdin with IO.readlines, each and split.
In a one-liner, or as a small source file? Doing that in a small source file is also common in Perl, but if you're doing that as a one-liner, I think you're not making it more readable, you're making it less so, as the executable/language specifically support features to make that easier to do which you're ignoring, obfuscating the important parts of the code with verbose obvious bits. It's sort of like making sure to spell out et cetera instead of using etc even when texting or using Twitter.
1: BEGIN actually has some important uses in Perl, but under the covers for most people, so they never have to deal with it.
This is very useful to script a setup -> for reach line -> summary of a file e.g. initialise a map in BEGIN, fill the map for each line, and print the map in END.
$ cat data.rb
DATA.each_line { |ln|puts ln }
__END__
fish
cat
dog
$ ruby data.rb
fish
cat
dogThe removal of the flip-flop operator, which is a fundamental tool in text processing, sent a very clear signal about the Ruby direction (that is, to be "The Rails language" and nothing else).
How can one be sure now, that all the functionality you've mentioned (and more) won't be removed as well?
Perl can be ugly, but its foundation in text processing are established.
Well that never happened. It was deprecated with the intention to remove it, but they backtracked: https://bugs.ruby-lang.org/issues/5400#note-25
Ruby (and Perl) can and probably should be used for many tasks where you might use awk and sed and don't particularly care about performance.
An argument "against readability" is culture. For sysadmins, Perl is a well-known language; it's not unreadable¹. Note that the same applies to Bash, and Bash scripts are still popular. This doesn't imply that Perl/Bash scripts should be universally used, but that within a certain culture, they make perfect sense.
¹=when talking about oneliners
There's plenty of stuff that is similar to Perl, which in turn had borrowed from awk, sed and other tools. The biggest difference I'd pick is Perl's context based operation.
# Ruby requires explicit conversion with to_i
ruby -ane 'print if $F[1].to_i > 35'
perl -ane 'print if $F[1] > 35'
Performance is slower compared to Perl, but to those who already know Ruby, this would suit better than learning the nuances of another language.[1] https://github.com/learnbyexample/Command-line-text-processi...
- automatic associative array instantiation
- the return value of regular expressions with a capture group is the value matched by it (in Ruby, the closest to this is `[/regex/, <group_i>]`)
[code lang=“ruby”]
((33..126).map { |i| i.chr }).to_a.shuffle[0..14].join
[/code]
File.readlines('/usr/share/dict/words').map(&:strip).shuffle.first(5).map(&:capitalize).join
=> "PanChloroformateParoticTillotBrushoff"14.times.map { rand(33..126).chr }.shuffle.join
At any rate, would probably want to use https://ruby-doc.org/stdlib-2.5.1/libdoc/securerandom/rdoc/S... for password generation.
Also, I think you might just want an alphabet and use sample: https://ruby-doc.org/core-2.5.1/Array.html#method-i-sample
# unsure if you need a:
require 'securerandom'
(33..126).sample(14, random: SecureRandom).map(&:chr).join
1.upto(100){|n|puts'FizzBuzz'[i=n**4%-15,i+13]||n}For example, just yesterday it was to parse a log, grep for specific lines, pull out a time and a number per line, display name, number, and some number of characters afterwards as a quick abnd dirty graph of the magnitude to make it obvious if there were major outliers and to try to see trends over time. Still a one-liner.
That said, if rails has a super simple default startup that could serve a file, then it might be useful to start that as a one liner in select circumstances.