Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    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.




If 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!


Each of those examples is doing different things. This rb script locks you into two of the options.


> 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}" }'




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: