2017-06-04 11:23:28 +00:00
|
|
|
# Ruby is a dynamic, reflective, object-oriented, general-purpose programming language
|
|
|
|
# ruby is a ruby interpreter
|
|
|
|
|
|
|
|
# invoke Ruby from the command line to run the script foo.rb
|
|
|
|
ruby foo.rb
|
|
|
|
|
|
|
|
# pass code as an argument
|
|
|
|
ruby -e 'puts "Hello world"'
|
|
|
|
|
|
|
|
# The -n switch acts as though the code you pass to Ruby was wrapped in the following:
|
|
|
|
# while gets
|
2020-01-25 05:04:13 +00:00
|
|
|
# # code here
|
|
|
|
# end
|
2017-06-04 11:23:28 +00:00
|
|
|
ruby -ne 'puts $_' file.txt
|
|
|
|
|
2019-10-15 08:55:32 +00:00
|
|
|
# Beware that with the -n switch $_ contains newline character in the end.
|
|
|
|
# With the addition of -l switch each line read has the newline character removed.
|
|
|
|
ls | ruby -lne 'File.rename($_, $_.upcase)'
|
|
|
|
|
2017-06-04 11:23:28 +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 $_
|
|
|
|
# Example: replace 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'
|