Fork me on GitHub

Project Notes

Ruby One-liners

All about one-liners with ruby.

Notes

The Ruby one-liners cookbook has it all!

Command Line Options

Using a ruby one-liner to format the ruby command line options as markdown for the table below…

ruby -h | ruby -F'  ' -ane 'puts $F[0].include?("Usage") ? "| Option | Description |\n|---|---|\n" : "| `#{$F[1]}` | #{$_.chomp.gsub($F[1],"").strip} |"'
Option Description
-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
-v print the version number, then turn on verbose mode
-w turn warnings on for your script
-W[level=2|:category] set warning level; 0=silence, 1=medium, 2=verbose
-x[directory] strip off text before #!ruby line and perhaps cd to directory
--jit enable JIT with default options (experimental)
--jit-[option] enable JIT with an option (experimental)
-h show this message, -elp for more info

Examples

See the Ruby one-liners GitHub source for a full suite of examples.

$ ./examples.sh
Regexp based filtering - should match /foo/a/
/foo/a/report.log

Credits and References

About LCK#252 ruby
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.