The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Taming $RUBYLIB with the Z shell

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Christian Neukirchen

Posts: 188
Nickname: chris2
Registered: Mar, 2005

Christian Neukirchen is a student from Biberach, Germany playing and hacking with Ruby.
Taming $RUBYLIB with the Z shell Posted: Aug 8, 2008 6:06 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Christian Neukirchen.
Original Post: Taming $RUBYLIB with the Z shell
Feed Title: chris blogs: Ruby stuff
Feed URL: http://chneukirchen.org/blog/category/ruby.atom
Feed Description: a weblog by christian neukirchen - Ruby stuff
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Christian Neukirchen
Latest Posts From chris blogs: Ruby stuff

Advertisement

Ok, I’m fed up. Writing a good package manager for Ruby is a fight against windmills.

So let’s do the easiest thing that could possibly work. Redefining Kernel#require is a no-go, for it will lead to the gates of hell. Installing multiple projects to the same location is error-prone, requires non-trivial amounts of code and introduces packaging effort.

Luckily, most packages these days run directly from a checkout or their released archives (and if you provide neither, you’re doing it wrong). Essentially, all you need to make it convenient setting and manipulating $RUBYLIB, “A colon-separated list of directories that are added to Ruby’s library load path ($:).” The Z shell (1, 2) to the rescue!

Add this to your .zshrc (or .zshenv, if you want it in non-interactive shells as well):

# unique, exported, tied array of $rubylib to colon-seperated $RUBYLIB
# 08aug2008  +chris+
typeset -T -U -gx -a RUBYLIB rubylib ':'
rubylib-add()   { rubylib+=("$@") }
rubylib-del()   { for i ("$@") { rubylib[(r)$i]=() } }
rubylib-reset() { rubylib=(); [[ -f ~/.rubylib ]] && source ~/.rubylib }
rubylib-reset

This creates a zsh array rubylib which value reflects $RUBYLIB and vice versa (zsh does the same for $PATH and $MANPATH, for example), and defines three functions to add and remove paths as well as reset the variable.

Also, create a file ~/.rubylib where you set the defaults. I simply use:

rubylib-add ~/projects/{testspec,bacon,rack}/lib
rubylib-add ~/src/{camping,markaby}/lib

Remember, you can use the full power of zsh to set this:

rubylib-add ~/src/rubystuff/*/(lib|ext)(/)

You need to use (x|y) instead of {x,y} here to only expand to existing files. The final (/) ensures these files really are directories.

Reload your .zshrc, and voila, your packages are accessible from every Ruby script. Now, if a project has different requirements, just create a script there to adjust $RUBYLIB. Or use vared to interactively change the load path.

[Thanks to et for improving rubylib-del.]

NP: Curve—Dirty High

Read: Taming $RUBYLIB with the Z shell

Topic: Blastwave Troubles Previous Topic   Next Topic Topic: RubyForge newcaster

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use