tags:

views:

52

answers:

1

I'm trying to include a source code file when I run irb but irb is unable to find it.

For example, say I am in the following directory in terminal:

/dan/rubyapp/

Assume I have a file named "firstapp.rb" in /dan/rubyapp/

I startup irb and from the irb prompt I type

> require "firstapp.rb"

but the file can't be found. If I type "Dir.pwd" it shows as

/dan/rubyapp/

The only way I can get "require" to work is if I include the full path like so

> require "/dan/rubyapp/firstapp.rb"

Is that the only way I can get this to work? All the tutorials I see online simply do "require file_name" so I assumed it would work.


here is the output from $: at irb

ruby-1.9.2-p0 > $:
 => ["/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/bin", 
"/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/lib", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.4.0", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.4.0", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby", 
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1",     
"/Users/Daniel/.rvm/rubies/ruby-
1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.4.0"] 
+1  A: 

Hi,

A couple of things to try:

1) Drop the .rb from the end of your require so you have:

require 'firstapp'

You don't normally add the .rb to a require (only to a load) - have a look here for more details: http://www.fromjavatoruby.com/2008/10/require-vs-load.html

2) Failing that, make sure the current directory is on your load path - in irb execute:

 p $:

and it will print out your ruby load path - check for an entry for "." (mine is the last entry)

Ash
is doing "$LOAD_PATH << '.'" in my .irbrc the best way to do this?
iljkj
So is . not in there? Could you show your output from p $:
Ash
Sorry, missed this comment. But yes the . was not there. I had to manually add it to my .irbc file via "$LOAD_PATH <<'.'". I'll post what my $: looked like before adding it in an edit to my question.
iljkj