This was the tip of the week in the October 14, 2021 Ruby Weekly Newsletter.

You will be familiar with running Ruby code saved in a file from the command line using ruby filename.rb. You’ll also be familiar, especially if you follow along with the Tip of the Week!, with running snippets of code within different Ruby REPLs, such as irb, pry, rails c or something else.

We can also run small snippets by passing the -e flag to ruby from the command line. For instance:

$ ruby -e "puts 1 + 3"

The -e flag can come in handy if we’re looking for a specific method, or just need a quick calculation, and don’t necessarily need to open up a full REPL.

This guide to Ruby ‘one liners’ provides a vast array of ways to use the -e flag along with others to achieve quite complex outcomes from the command line or shell script.

Another useful flag is -c which will do a Ruby syntax check on a file without actually executing the file. I find this useful when resolving git merge conflicts, as a way to double check that I haven’t left any stray lines in my Ruby files. For instance:

$ ruby -c some_file.rb
some_file.rb:1: syntax error, unexpected end-of-input

The -e and -c flags are two of many which we can use when running ruby from the command line. To see more options, run man ruby from the command line, and we will cover some more interesting ones in coming weeks too.