Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails irb default directory

Tags:

ruby

irb

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"] 
like image 531
iljkj Avatar asked Sep 21 '10 10:09

iljkj


2 Answers

The problem is that the current working directory is no longer in your path (as of Ruby 1.9.2). There are a few different ways around the problem.

1) In a ruby file itself, you can use the method require_relative instead of require. This will load a file relative to the loaction of the file containing the require_relative method: http://extensions.rubyforge.org/rdoc/classes/Kernel.html

require_relative 'firstapp.rb'

This, however, will not work in irb.

2) Your other option is to include the current path in your argument to the require method. This will work in irb or in a ruby file. For instance:

require './firstapp.rb'

The reason this was implemented in ruby was to avoid inadvertently requiring the wrong file if there are different files with the same name in different directories in the path (similar to how *nix does not include the current directory "." in its path)

like image 85
Johnny Gannon Avatar answered Nov 15 '22 12:11

Johnny Gannon


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)

like image 41
Ash Avatar answered Nov 15 '22 14:11

Ash