diff --git a/sheets/ruby b/sheets/ruby index 848498e..03db285 100644 --- a/sheets/ruby +++ b/sheets/ruby @@ -1,26 +1,27 @@ -# Ruby is a dynamic, reflective, object-oriented, general-purpose programming language -# ruby is a ruby interpreter +# ruby +# Interpreter of object-oriented scripting language Ruby -# invoke Ruby from the command line to run the script foo.rb +# Invoke Ruby; a dynamic, reflective, object-oriented, general-purpose +# programming language; from the command line to run the provided script. ruby foo.rb -# pass code as an argument +# Execute Ruby code directly from the command-line. ruby -e 'puts "Hello world"' -# The -n switch acts as though the code you pass to Ruby was wrapped in the following: -# while gets -# # code here -# end +# 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 in the end. -# With the addition of -l switch each line read has the newline character removed. +# 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 $_ -# Example: replace e with a +# 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 +# BEGIN block executed before the loop. echo "foo\nbar\nbaz" | ruby -ne 'BEGIN { i = 1 }; puts "#{i} #{$_}"; i += 1'