Show HN: Rb – Turns Ruby into a command line utility
1–10 of 79 posts
Re: Show HN: Rb – Turns Ruby into a command line utility
#2Re: Show HN: Rb – Turns Ruby into a command line utility
#3I have no idea what this is doing, but it sure looks interesting.
Re: Show HN: Rb – Turns Ruby into a command line utility
#4I have no idea what this is doing, but it sure looks interesting.
docker ps | rb drop 1 | rb -l split[1]
The output of docker ps is passed as an array of lines to ruby, and the first line is dropped, and then that output is fed to ruby again, but line by line, so each line is split (which defaults to split on whitespace), and returns the second element. With parentheses for function calls it would be: docker ps | rb drop(1) | rb -l split()[1]Re: Show HN: Rb – Turns Ruby into a command line utility
#5cat your-file.txt | ruby -ne '$_.your_ruby_stuff'
Just syntactic sugar? (it does look cleaner!)
Re: Show HN: Rb – Turns Ruby into a command line utility
#6I have no idea what this is doing, but it sure looks interesting.
It's feeding stdin to ruby code you specify on the command line, either as an array of lines, or repeated strings. So in: docker ps | rb drop 1 | rb -l split[1] The output of docker ps is passed as an array of lines to ruby, and the first line is dropped, and then that output is fed to ruby again, but line by line, so each line is split (which defaults to split on whitespace), and returns the second element. With par…
Re: Show HN: Rb – Turns Ruby into a command line utility
#7 docker ps | rb drop 1 | rb -l split[1]
docker ps -a | rb grep /Exited/ | rb -l 'split.last.ljust(20) + " => " + split(/ {2,}/)[-2]'
df -h | rb 'drop(1).sort_by { |l| l.split[-2].to_f }'
Command line tools in zero lines of ruby (using ruby): docker ps | ruby -lane 'next if $. == 1; print $F[1]
docker ps -a | ruby -lne 'print $1 if /(Exited .*? ago)/'
df | ruby -lane 'BEGIN { lines = [] }; lines.push [$F[4].to_i, $_] if $. > 0; END { lines.sort { |a,b|b[0] a[0] }.each{|k| print k[1] } }
Bit of a pain as ++ is lacking, as is autovivication. a /bin/sort at the end usually beats `` for terseness.Re: Show HN: Rb – Turns Ruby into a command line utility
#8What does this have over the default ruby? cat your-file.txt | ruby -ne '$_.your_ruby_stuff' Just syntactic sugar? (it does look cleaner!)
I know this is ancient history.
Re: Show HN: Rb – Turns Ruby into a command line utility
#9A few examples:
Capitalizing a line:
echo hello world | rb -l capitalize
Printing only unique lines: printf 'hello\nworld\nhello\n' | rb uniq
Computing the sum of numbers in a table: printf '1\n2\n3\n' | rb 'map(&:to_i).sum'
Personally I think it's a really cool idea.