How to get piped input to ruby -e on command line? How to get piped input to ruby -e on command line? bash bash

How to get piped input to ruby -e on command line?


Ruby treats your line as a comment because it starts with a #.

This would work:

echo "My String" | ruby -e "puts gets.downcase"

Output:

my string

I've used Kernel#gets instead of STDIN.gets:

Returns (and assigns to $_) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line

If you want to process each line, you could use the -p flag. It's like wrapping your script in a while gets(); ... end; puts $_ block. Ruby reads each input line into $_, evaluates your script and outputs $_ afterwards:

echo "Foo\nBar\nBaz" | ruby -pe '$_.downcase!'

Output:

foobarbaz


Just

echo "My String" | ruby -ne 'puts $_.downcase'

or

echo "My String" | ruby -e "puts gets.downcase"

You get the idea.