|
I don’t usually talk must about one-liners, but here’s one I used today.
ARGF and inject ... Two Great Tastes that Taste Great Together
Consider the following ruby one-lineer
ruby -e 'p ARGF.inject(0) { |sum, line| line.split[1].to_i + sum }'
Breaking it down, it …
- Iterates over each line of standard input (
ARGF.inject(0))
- Splits the line into colums (
line.split)
- Selects the second column (
[1])
- Converts the second column to an integer (
.to_i)
- Adds to to the running subtotal (
+ sum)
- And prints the final sum (
p)
In short, it sums up the numbers in the second column of a text file.
Cool. Now combine it with another one-liner …
lynx -dump http://gems.rubyforge.org/stats.html \
| ruby -e 'p ARGF.inject(0) { |s,t| t.split[1].to_i + s }'
And you get the result …
1005821
... which is the number of gems served by RubyForge to date.
Over 1 million gems served :)
blog comments powered by
|