Rails irb default directory
Asked Answered
G

2

6

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"] 
Gans answered 21/9, 2010 at 10:0 Comment(0)
P
2

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)

Padrone answered 21/9, 2010 at 10:43 Comment(2)
is doing "$LOAD_PATH << '.'" in my .irbrc the best way to do this?Gans
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.Gans
S
8

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)

Sanskritic answered 18/1, 2011 at 21:54 Comment(1)
This helped me! In order for mine to work, however, I had to use a forward slash, so require './firstapp.rb' would be more appropriate.Behlau
P
2

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)

Padrone answered 21/9, 2010 at 10:43 Comment(2)
is doing "$LOAD_PATH << '.'" in my .irbrc the best way to do this?Gans
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.Gans

© 2022 - 2024 — McMap. All rights reserved.