4 ms·
echo hello world | ruby -pe '$_.capitalize!' printf 'hello\nworld\nhello\n' | ruby -le 'puts STDIN.to_a.uniq ps | ruby -lane 'BEGIN { b = 0 }; b += $F
by star-techate 8y ago
echo hello world | ruby -pe '$_.capitalize!'
printf 'hello\nworld\nhello\n' | ruby -le 'puts STDIN.to_a.uniq
ps | ruby -lane 'BEGIN { b = 0 }; b += $F[0].to_i; END { print "sum of PIDs: #{b}" }
select whatever column you want, to sum up.
Really what Ruby needs is a flag that adds some capabilities to NilClass, so that these BEGIN blocks aren't needed.
- ricardobeat 8y agoIf this was an attempt at showing this tool is unnecessary, you just did the opposite. Each of your examples use different flags, inputs and print methods. The last is particularly good at proving the point!
- ashelmire 8y agoEach of those examples is doing different things. This rb script locks you into two of the options.
- dragonwriter 8y ago> Really what Ruby needs is a flag that adds some capabilities to NilClass, so that these BEGIN blocks aren't needed. Does it, though? ps | ruby -le 'b = $<.reduce(0) {|s, l| s + l.split(0).to_i}; print "sum of PIDs: #{b}"' or: ps | ruby -lane 'b = (b || 0) + $F[0].to_i; END { print "sum of PIDs: #{b}" }'