The idiomatic counterpart to Ruby `Object # tap` for Unix Pipelines commands?

Is there an idiomatic Ruby equivalent Object#tapfor Unix command pipelines?

Use case: in the pipeline I want to execute a command for my side effects, but implicitly return the input so as not to break the chain of the pipeline. For instance:

echo { 1, 2, 3 } |
  tr ' ' '\n' |
  sort |
  tap 'xargs echo' | # arbitrary code, but implicitly return the input
  uniq

I come from Ruby where I would do this:

[ 1, 2, 3 ].
  sort.
  tap { |x| puts x }.
  uniq
+5
source share
1 answer

The team teeis similar; it writes its input to standard output, as well as one or more files. If this file is a replacement for the process, you will get the same effect, I suppose.

echo 1 2 3 | tr ' ' '\n' | sort | tee >( **code** ) | uniq

The code in the process substitution will be read from standard input, which should coincide with the fact that the call uniqends with viewing.

+6
source

All Articles