[SVN] Re: Ruby's Require statement is broken

James Gray james at grayproductions.net
Tue Oct 28 13:57:49 UTC 2008


On Oct 28, 2008, at 6:48 AM, Stephen Malone wrote:

> I am coding an IRC bot in Ruby, and the require statement does not
> work within Textmate. Using "ruby bot.rb" in the Terminal, the program
> operates fine, but in Textmate's own RubyMate environment (which is
> initialized with Cmd+R), any require statement that loads a file
> outside of Ruby's standard library (i.e.: a file you've made yourself)
> will return the LoadError error message.

Ruby has a "load path" which is a list of directories it will load  
files from.  As Allan's message showed, you can see this list in the  
variables $: or $LOAD_PATH.  By default, "." or the current working  
directory is in Ruby's load path.

Thus, you are probably using a relative require and when you are in  
the correct directory inside the Terminal, it works.  TextMate  
probably isn't running you code from the same directory and thus the  
require fails.

This is something you could easily test.  For example, if your bot.rb  
file is in a directory structure like /Users/stephen/Documents/ 
projects/irc_bot/ (I'm totally guessing, so use the real directories),  
try running it from a higher level and see if it breaks:

   cd /Users/stephen/Documents/projects
   ruby irc_bot/bot.rb

Probably the best way to fix this is to modify your code to prepare  
for this.  You can do that by making sure bot.rb adds the directory it  
is in to the load path.  So if bot.rb has requires like:

   require "a"
   require "b"

You could just make sure the load path is changed before those files  
are required:

   $LOAD_PATH << File.dirname(__FILE__)
   require "a"
   require "b"

Hope that helps.

James Edward Gray II



More information about the textmate-dev mailing list