Ruby command line



Przemysław::Kowalczyk
a.k.a. szeryf

ruby -‍-help

Usage: ruby [switches] [--] [programfile] [arguments]
  -0[octal]       specify record separator (\0, if no argument)
  -a              autosplit mode with -n or -p (splits $_ into $F)
  -c              check syntax only
  -Cdirectory     cd to directory, before executing your script
  -d              set debugging flags (set $DEBUG to true)
  -e 'command'    one line of script. Several -e's allowed. Omit [programfile]
  -Eex[:in]       specify the default external and internal character encodings
  -Fpattern       split() pattern for autosplit (-a)
  -i[extension]   edit ARGV files in place (make backup if extension supplied)
  -Idirectory     specify $LOAD_PATH directory (may be used more than once)
  -l              enable line ending processing
  -n              assume 'while gets(); ... end' loop around your script
  -p              assume loop like -n but print line also like sed
  -rlibrary       require the library, before executing your script
  -s              enable some switch parsing for switches after script name
  -S              look for the script using PATH environment variable
  -T[level=1]     turn on tainting checks
  -v              print version number, then turn on verbose mode
  -w              turn warnings on for your script
  -W[level=2]     set warning level; 0=silence, 1=medium, 2=verbose
  -x[directory]   strip off text before #!ruby line and perhaps cd to directory
  --copyright     print the copyright
  --version       print the version
    

lots of cryptic options
...but some look familiar

Usage: perl [switches] [--] [programfile] [arguments]
  -0[octal]         specify record separator (\0, if no argument)
  -a                autosplit mode with -n or -p (splits $_ into @F)
  -C[number/list]   enables the listed Unicode features
  -c                check syntax only (runs BEGIN and CHECK blocks)
  -d[:debugger]     run program under debugger
  -D[number/list]   set debugging flags (argument is a bit mask or alphabets)
  -e program        one line of program (several -e's allowed, omit programfile)
  -E program        like -e, but enables all optional features
  -f                don't do $sitelib/sitecustomize.pl at startup
  -F/pattern/       split() pattern for -a switch (//'s are optional)
  -i[extension]     edit <> files in place (makes backup if extension supplied)
  -Idirectory       specify @INC/#include directory (several -I's allowed)
  -l[octal]         enable line ending processing, specifies line terminator
  -[mM][-]module    execute "use/no module..." before executing program
  -n                assume "while (<>) { ... }" loop around program
  -p                assume loop like -n but print line also, like sed
  -P                run program through C preprocessor before compilation
  -s                enable rudimentary parsing for switches after programfile
  -S                look for programfile using PATH environment variable
  -t                enable tainting warnings
  -T                enable tainting checks
  -u                dump core after parsing program
  -U                allow unsafe operations
  -v                print version, subversion (includes VERY IMPORTANT perl info)
  -V[:variable]     print configuration summary (or a single Config.pm variable)
  -w                enable many useful warnings (RECOMMENDED)
  -W                enable all warnings
  -x[directory]     strip off text before #!perl line and perhaps cd to directory
  -X                disable all warnings
    
Usage: perl [switches] [--] [programfile] [arguments]
  -0[octal]         specify record separator (\0, if no argument)
  -a                autosplit mode with -n or -p (splits $_ into @F)
  -C[number/list]   enables the listed Unicode features
  -c                check syntax only (runs BEGIN and CHECK blocks)
  -d[:debugger]     run program under debugger
  -D[number/list]   set debugging flags (argument is a bit mask or alphabets)
  -e program        one line of program (several -e's allowed, omit programfile)
  -E program        like -e, but enables all optional features
  -f                don't do $sitelib/sitecustomize.pl at startup
  -F/pattern/       split() pattern for -a switch (//'s are optional)
  -i[extension]     edit <> files in place (makes backup if extension supplied)
  -Idirectory       specify @INC/#include directory (several -I's allowed)
  -l[octal]         enable line ending processing, specifies line terminator
  -[mM][-]module    execute "use/no module..." before executing program
  -n                assume "while (<>) { ... }" loop around program
  -p                assume loop like -n but print line also, like sed
  -P                run program through C preprocessor before compilation
  -s                enable rudimentary parsing for switches after programfile
  -S                look for programfile using PATH environment variable
  -t                enable tainting warnings
  -T                enable tainting checks
  -u                dump core after parsing program
  -U                allow unsafe operations
  -v                print version, subversion (includes VERY IMPORTANT perl info)
  -V[:variable]     print configuration summary (or a single Config.pm variable)
  -w                enable many useful warnings (RECOMMENDED)
  -W                enable all warnings
  -x[directory]     strip off text before #!perl line and perhaps cd to directory
  -X                disable all warnings
    
Usage: ruby [switches] [--] [programfile] [arguments]
  -0[octal]       specify record separator (\0, if no argument)
  -a              autosplit mode with -n or -p (splits $_ into $F)
  -c              check syntax only
  -Cdirectory     cd to directory, before executing your script
  -d              set debugging flags (set $DEBUG to true)
  -e 'command'    one line of script. Several -e's allowed. Omit [programfile]
  -Eex[:in]       specify the default external and internal character encodings
  -Fpattern       split() pattern for autosplit (-a)
  -i[extension]   edit ARGV files in place (make backup if extension supplied)
  -Idirectory     specify $LOAD_PATH directory (may be used more than once)
  -l              enable line ending processing
  -n              assume 'while gets(); ... end' loop around your script
  -p              assume loop like -n but print line also like sed
  -rlibrary       require the library, before executing your script
  -s              enable some switch parsing for switches after script name
  -S              look for the script using PATH environment variable
  -T[level=1]     turn on tainting checks
  -v              print version number, then turn on verbose mode
  -w              turn warnings on for your script
  -W[level=2]     set warning level; 0=silence, 1=medium, 2=verbose
  -x[directory]   strip off text before #!ruby line and perhaps cd to directory
  --copyright     print the copyright
  --version       print the version
    

Is Ruby influenced by Perl?

I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. (...)

Ruby's class library is an object-oriented reorganization of Perl functionality--plus some Smalltalk and Lisp stuff.

Yukihiro Matsumoto a.k.a. "Matz"

http://www.flickr.com/photos/32513189@N03/6064652520

basic options

ruby --version
ruby -v

ruby -c
ruby -w

TextMate's Ruby bundle runs
ruby -cw
on save

http://www.flickr.com/photos/kenb/1398914855

slightly advanced options

ruby -e 'command'
one line of script. Several -e's allowed. Omit [programfile]

ruby -Idirectory
specify $LOAD_PATH directory (may be used more than once)
ruby -rlibrary
require the library, before executing your script

Fun fact #1:
require 'ubygems' actually works!

http://www.flickr.com/photos/andrewdewolfe/865858309

moderately convoluted options

ruby -s enable some switch parsing for switches after script name (does not work with -e)

Might be actually useful... my-script.rb -verbose

ruby -Cdirectory cd to directory, before executing your script

http://www.flickr.com/photos/alykat/4895929410

higher driving school

ruby -n assume 'while gets(); ... end' loop around your script

How does it work?

becomes

which is equivalent to

ruby -p assume loop like -n but print line also like sed

How does it work?

becomes * gsub(...){...} does not work with -n on Ruby MRI 1.9.3, works on 1.8.7

Ruby defines special versions of chomp, chop, gsub, sub in loop mode

More examples

Inserts empty line before each line

Inserts empty line after each line

Strip trailing whitespace from each line

Print first 10 lines of file.

Even more examples

Print lines between line containing <body> and line containing </body>

ROT13 like a (Ruby) boss!

ruby -l enable line ending processing

source:wikipedia

ninjas only

ruby -a autosplit mode with -n or -p (splits $_ into $F)

ruby -Fpattern split() pattern for autosplit (-a)

BEGIN & END blocks

More fun with BEGIN & END

source:wikipedia

you've got to be kidding

ruby -0[octal] specify record separator (\0, if no argument)

ruby -0777 read whole file at once as a single string

ruby -00 read file in paragraph mode

Mastering -l-fu & -0jitsu

ruby -i[extension] edit ARGV files in place (make backup if extension supplied)

ruby -i[extension] edit ARGV files in place (make backup if extension supplied)

source:wikipedia

Thank you!

Questions?

Where do I find...

/