2020-10-20 22:47:15 +00:00
|
|
|
# ruby
|
|
|
|
# Interpreter of object-oriented scripting language Ruby
|
2017-06-04 11:23:28 +00:00
|
|
|
|
2020-10-20 22:47:15 +00:00
|
|
|
# Invoke Ruby; a dynamic, reflective, object-oriented, general-purpose
|
|
|
|
# programming language; from the command line to run the provided script.
|
2017-06-04 11:23:28 +00:00
|
|
|
ruby foo.rb
|
|
|
|
|
2020-10-20 22:47:15 +00:00
|
|
|
# Execute Ruby code directly from the command-line.
|
2017-06-04 11:23:28 +00:00
|
|
|
ruby -e 'puts "Hello world"'
|
|
|
|
|
2020-10-20 22:47:15 +00:00
|
|
|
# The `-n` switch allows Ruby to execute code within a `while gets` loop.
|
2017-06-04 11:23:28 +00:00
|
|
|
ruby -ne 'puts $_' file.txt
|
|
|
|
|
2020-10-20 22:47:15 +00:00
|
|
|
# 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.
|
2019-10-15 08:55:32 +00:00
|
|
|
ls | ruby -lne 'File.rename($_, $_.upcase)'
|
|
|
|
|
2020-10-20 22:47:15 +00:00
|
|
|
# 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`.
|
2017-06-04 11:23:28 +00:00
|
|
|
echo "eats, shoots, and leaves" | ruby -pe '$_.gsub!("e", "a")'
|
|
|
|
|
2020-10-20 22:47:15 +00:00
|
|
|
# BEGIN block executed before the loop.
|
2017-06-04 11:23:28 +00:00
|
|
|
echo "foo\nbar\nbaz" | ruby -ne 'BEGIN { i = 1 }; puts "#{i} #{$_}"; i += 1'
|