# ruby # Interpreter of object-oriented scripting language Ruby # Invoke Ruby; a dynamic, reflective, object-oriented, general-purpose # programming language; from the command line to run the provided script. ruby foo.rb # Execute Ruby code directly from the command-line. ruby -e 'puts "Hello world"' # The `-n` switch allows Ruby to execute code within a `while gets` loop. ruby -ne 'puts $_' file.txt # Beware that with the `-n` switch, `$_` contains newline character at the end. # With the addition of the `-l` switch, each line read has the aforementioned # newline character removed. ls | ruby -lne 'File.rename($_, $_.upcase)' # The `-p` switch acts similarly to `-n`, in that it loops over each of the # lines in the input, after your code has finished; it always prints the value # of `$_`. # # The following example replaces `e` with `a`. echo "eats, shoots, and leaves" | ruby -pe '$_.gsub!("e", "a")' # BEGIN block executed before the loop. echo "foo\nbar\nbaz" | ruby -ne 'BEGIN { i = 1 }; puts "#{i} #{$_}"; i += 1'