Ruby command -- bash readline completion

I have put together some basic bash readline completion for ruby.

* Typing `ruby -[TAB]` will give you a list of available command line switches.
* Typing `ruby -r[TAB]' will give you a list of available libraries to load. For this I have disabled searching "." because that could take a very long time.

See [http://www.rubygarden.org/ruby?ProgrammableCompletition] for ri completion

Create a file named ruby in bash_completion.d/ with --

···

#+++++++
have ruby &&
{
   # Get a list of all available ruby libraries
   _ruby_libraries()
   {
     COMPREPLY=( $( compgen -P "$prefix" -W "$( ruby -rfind -e '
     libraries = []
     $:.each do |path|
       # searching "." may take a very long time.
       unless path == "."
         Find.find(path) do |found|
           Find.prune if File.dirname(found) =~ /test/
           libraries << found.gsub(/^#{path}/,"")[1..-4] if found[/.rb$/]
         end
       end
     end
     print libraries.join(" ")
   '
     ) " -- $cur ) )
   }

   _ruby()
   {
       local cur prev prefix temp

       COMPREPLY=()
       cur=${COMP_WORDS[COMP_CWORD]}
       prev=${COMP_WORDS[COMP_CWORD-1]}
       prefix=""

       # completing an option (may or may not be separated by a space)
       if [[ "$cur" == -?* ]]; then
         temp=$cur
         prev=${temp:0:2}
         cur=${temp:2}
         prefix=$prev
       fi

       case "$prev" in
         -r)
           _ruby_libraries
           return 0
         ;;
       esac

       # handle case where first parameter is not a dash option
       if [ $COMP_CWORD -eq 1 ] && [[ "$cur" != -* ]]; then
         _filedir
         return 0
       fi

       # complete using basic options
       COMPREPLY=( $( compgen -W '-a -c -d -e -F -l -n -p -s -S -T -v -w -W -0 -C -i -I -K -r' -- $cur ) )

       return 0
   }
   complete -F _ruby $default ruby
}
#+++++++

-- Daniel