ruby.onl / one-liners

Ruby's Command-Line Switches: Everything Perl Taught You Still Works

2026-03-12

If you know perl -ne, you already know ruby -ne. That's not an exaggeration. Ruby supports most of Perl's one-liner switches with identical behavior. Here's the complete field guide to every switch that matters.

Part 1: The Essential Switches

-e 'code'

Execute code from the command line. Like Perl's -e.
ruby -e 'puts "hello"' # Perl: perl -e 'print "hello\n"'

-n

Wraps your code in while gets; ...; end. Reads input line by line into $_. Like Perl's -n.
ruby -ne 'puts $_ if /ERROR/' logfile.txt # Perl: perl -ne 'print if /ERROR/' logfile.txt

-p

Like -n but automatically prints $_ after each iteration. Like Perl's -p.
ruby -pe '$_.upcase!' file.txt # Perl: perl -pe '$_ = uc' file.txt

-a

Auto-split mode. Splits each line into the $F array. Like Perl's -a with @F.
ruby -ane 'puts $F[2]' data.txt # Perl: perl -ane 'print $F[2], "\n"' data.txt

-F delimiter

Set the field separator for -a. Like Perl's -F.
ruby -F, -ane 'puts $F[1]' data.csv # Perl: perl -F, -ane 'print $F[1], "\n"' data.csv # Tab-separated: ruby -F'\t' -ane 'puts $F[0]' data.tsv # Colon-separated (like /etc/passwd): ruby -F: -ane 'puts $F[0]' /etc/passwd

Part 2: File Editing and I/O Switches

-i[extension]

In-place editing. With extension, creates backup. Like Perl's -i.
# In-place edit with .bak backup ruby -i.bak -pe '$_.gsub!(/old/, "new")' file.txt # Perl: perl -i.bak -pe 's/old/new/g' file.txt # In-place edit without backup (dangerous!) ruby -i -pe '$_.gsub!(/old/, "new")' file.txt

-l

Auto-chomp input and auto-add newline to output. Like Perl's -l.
ruby -lne 'puts $_.reverse' file.txt # Perl: perl -lne 'print reverse' file.txt

-0[octal]

Set input record separator. -0 sets it to null (slurp paragraphs). -0777 slurps entire file. Like Perl's -0.
# Slurp entire file ruby -0777 -ne 'puts $_.scan(/pattern/).length' file.txt # Null-terminated records ruby -0 -ne 'puts $_' file.txt

Part 3: Library and Debug Switches

-r library

Require a library before executing. Like Perl's -M.
ruby -rjson -e 'puts JSON.parse(STDIN.read).keys' # Perl: perl -MJSON -e '...'

-w

Enable warnings. Good practice for scripts.
ruby -wne 'puts $_ if /pattern/' file.txt

Part 4: Combining Switches

Switches can be combined just like in Perl:
# Split on comma, print second field, auto-chomp ruby -F, -lane 'puts $F[1]' data.csv # In-place edit with backup, auto-chomp, print modified line ruby -i.bak -lpe '$_.gsub!(/old/, "new")' file.txt # Count pattern matches across files ruby -ne 'BEGIN{$n=0}; $n+=1 if /error/i; END{puts $n}' *.log

Part 5: The One Key Difference

In Perl, -p and s/// work together naturally: perl -pe 's/old/new/g'.

In Ruby, you need the bang method to modify $_ in place: ruby -pe '$_.gsub!(/old/, "new")'.

Without the !, gsub returns a new string and $_ stays unchanged, so nothing visibly happens. You'll forget this. Everyone forgets this. Tattoo it somewhere visible.


Created By: Wildcard Wizard. Copyright 2026