Home / Alt manpages / ruby3.2(1)

  • ruby3.2(1)
  • User command
  • linux

Use ruby3.2 for Safe One-Liners and Script Checks

You will use the installed ruby3.2 command to run a short Ruby program, check a file without executing it, process text from standard input, and make a controlled in-place edit with a backup. The examples match the machine used for this guide: Ruby 3.2.3 from the ruby3.2 package.

Allow about 15 minutes. You need a shell and a writable working directory. None of the normal examples need elevated privileges. Do not use sudo merely because a Ruby command fails; first check the path, permissions and Ruby error.

Checkpoint 1: confirm the interpreter

  1. Check which executable will run and record its version.
command -v ruby3.2
ruby3.2 --version
dpkg-query -W -f='${Package} ${Version}\n' ruby3.2

On the reference system, the output identifies Ruby 3.2.3 and package version 3.2.3-1ubuntu0.24.04.8. Your package revision may differ after normal system updates. The unversioned ruby command is an alias or separately selected interpreter on some systems, so use ruby3.2 when this guide's version matters.

Verify: the version begins with ruby 3.2., and command -v points to the executable you expect.

Checkpoint 2: run a small program

  1. Pass Ruby code with -e and quote it so the shell does not interpret the code first.
ruby3.2 -e 'puts "Ruby #{RUBY_VERSION} is running"'

Expected output is similar to:

Ruby 3.2.3 is running

You can provide more than one -e option. Ruby executes them in order:

ruby3.2 -e 'name = "Ada"' -e 'puts "Hello, #{name}"'

Keep shell quoting visible. Single quotes are convenient for a Ruby snippet containing double-quoted strings. If the program itself needs a single quote, either switch the shell quoting or put the program in a file. A command such as ruby3.2 -e 'puts "$HOME"' prints the literal shell variable text because the shell does not expand inside single quotes.

Checkpoint 3: check syntax without running code

  1. Use -c against a script before you run it.
ruby3.2 -c /path/to/example.rb

A valid script prints:

Syntax OK

The check parses the program and exits without executing it. It does not prove that files, gems, services or external commands used by the program will work. Treat a syntax pass as one checkpoint, not a security review.

If you are testing a snippet rather than a file, this is safe because the body is only parsed:

printf '%s\n' 'puts "not executed"' | ruby3.2 -c /dev/stdin

Recovery

If the command reports a syntax error, edit the script and repeat the check. No program state changes during -c.

Checkpoint 4: transform text through standard input

  1. Use -p with -e for a line-by-line transformation that prints each resulting line.
printf '%s\n' 'alpha' 'beta' | ruby3.2 -p -e '$_.upcase!'

Expected output:

ALPHA
BETA

-p wraps your code in a loop around gets and prints $_ after each iteration. Use -n instead when your code should inspect or emit output selectively:

printf '%s\n' 'keep this' 'skip this' | ruby3.2 -n -e 'puts $_ if /keep/'

For named files, append them after the Ruby options:

ruby3.2 -n -e 'puts $_ if /ERROR/' /path/to/application.log

Add -a to split each input line into fields in $F. Add -F, when commas separate the fields:

printf '%s\n' 'Ada,42' 'Linus,55' | ruby3.2 -F, -ane 'puts "#{$F[0]}: #{$F[1]}"'

The compact form is deliberate: the pattern immediately follows -F, while the Ruby code follows -e.

Checkpoint 5: edit a disposable copy, not the original

  1. Make a copy, then use -i.bak so Ruby keeps the old contents beside the edited file.
cp /path/to/notes.txt /tmp/notes.txt
ruby3.2 -p -i.bak -e '$_.gsub!(/OLD_VALUE/, "NEW_VALUE")' /tmp/notes.txt
diff -u /tmp/notes.txt.bak /tmp/notes.txt

-i.bak writes transformed output back to each named file and saves the previous version with the .bak suffix. The command can change every matching line, so inspect the diff before using it on a real file. The example changes only a temporary copy and is therefore reversible.

Warning

Do not use bare -i until you understand the consequence. It enables in-place mode without the explicit backup suffix. For a recovery, restore the backup after checking that it is the expected file:

cp -- /tmp/notes.txt.bak /tmp/notes.txt
cmp -- /tmp/notes.txt.bak /tmp/notes.txt

Useful boundaries and defaults

-C /path changes Ruby's working directory for the invoked program, not the shell that launched it:

ruby3.2 -C /tmp -e 'puts Dir.pwd'

-I /path/to/lib adds a directory to Ruby's load path, and -r library requires a library before the program starts. Only add directories and libraries you trust. The RUBYOPT environment variable can add interpreter options automatically; inspect it when a command behaves differently from its visible arguments:

printf 'RUBYOPT=%s\n' "${RUBYOPT-}"
ruby3.2 --disable-rubyopt -e 'puts "RUBYOPT ignored for this run"'

RubyGems is enabled by default in this installation. Use --disable-gems only when you specifically need an interpreter run without RubyGems integration. Do not copy environment settings from an unrelated shell session into a service without reviewing their effect.

Done means

  • ruby3.2 --version reports the intended 3.2 interpreter.
  • -e runs a quoted one-liner and produces the expected output.
  • -c reports Syntax OK before a script is executed.
  • -n and -p process input as intended.
  • Any in-place edit was made on a disposable copy, with a backup and an inspected diff.