0

When I do require './primes.rb' in irb I get this:

  1.9.3-p392 :004 > require './primes.rb'
LoadError: cannot load such file -- ./primes.rb
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):4
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'

Here is the primes.rb document:

# primes.rb
require 'debugger'

def prime?(num)
debugger
  (1..num).each do |i|
    if (num % i) == 0
      return false
    end
  end
end

def primes(num_primes)
  ps = []
  num = 1
  while ps.count < num_primes
    primes << num if prime?(num)
  end
end

if __FILE__ == $PROGRAM_NAME
  puts primes(100)
end

Any suggestions of how to get this to work would be greatly appreciated!

When I do require relative it gives me this:

1.9.3-p392 :010 > require_relative 'primes.rb'
LoadError: cannot infer basepath
    from (irb):10:in `require_relative'
    from (irb):10
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'

When I do the second solution below it gives me this:

1.9.3-p392 :013 > $LOAD_PATH << "."
 => ["/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/x86_64-darwin11.4.2", "."] 
1.9.3-p392 :014 > require 'primes.rb'
LoadError: cannot load such file -- primes.rb
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):14
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
1.9.3-p392 :015 > 

When I try it in pry:

[4] pry(main)> require_relative 'primes.rb'
LoadError: cannot infer basepath
from (pry):2:in `require_relative'
[5] pry(main)> require 'primes.rb'
LoadError: cannot load such file -- primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
[6] pry(main)> .ls
Applications        Movies          git-completion.bash
Desktop         Music           rails_projects
Documents       Pictures        ruby
Downloads       Public          runwithfriends
Dropbox         code            shopify
Library         dev         sites
[7] pry(main)> require 'ruby/app_acad_mini_curriculum/debugging/primes.rb'
LoadError: cannot load such file -- ruby/app_acad_mini_curriculum/debugging/primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
Ryan Bonhardt
  • 121
  • 1
  • 1
  • 7
  • Please trim down your question to a reasonable size. For example, there is absolutely no need to post the source code of `primes.rb`. Since it isn't even *found*, the error cannot possibly be in there anyway. Also, you haven't given any indication in the question as to whether the file `primes.rb` actually exists and whether it is in the directory you think it is in. – Jörg W Mittag May 07 '13 at 21:13

2 Answers2

0

Try require_relative

require_relative 'primes.rb'

EDIT: Note that this will only work from within a script. If you are trying to require this script into an irb session then you will need to provide the full path to primes.rb. The reason is where irb's location is. For instance, try Dir.pwd inside irb and you will see where require_relative is attempting to search for primes.rb.

There are a couple things you could do:

# Just need to require the one file.
require_relative File.join('users', 'yourusername', 'prime_folder', 'prime.rb')

# Many files in the same folder
$LOAD_PATH << File.join('users', 'yourusername', 'prime_folder')
require 'prime.rb'
require 'another_file.rb'

Another option, one that I use, is Pry. It is like irb and is very easy to call from a script. It is a gem so:

gem install pry

At the end of your script, you could do:

if $0 == __FILE__
  require 'pry'
  binding.pry
end

You would then drop into an irb like REPL where you can test and debug your methods. I can't survive without it.

Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
  • I updated my results above when I try this? Any other suggestions? – Ryan Bonhardt May 07 '13 at 19:06
  • It is because you are trying to require it in irb. I'm looking at a possible solution at the moment. – Charles Caldwell May 07 '13 at 19:07
  • Thank you. What I'm really trying to do is complete this exercise: https://github.com/appacademy/prep-work/blob/master/mini-curriculum/debugging/debugger.md – Ryan Bonhardt May 07 '13 at 19:13
  • awesome. I'm using pry and in it now but I'm getting the same problem. I've updated my prime file above and here is the error messages currently: [5] pry(main)> require 'primes.rb' LoadError: cannot load such file -- primes.rb [7] pry(main)> require 'ruby/app_acad_mini_curriculum/debugging/primes.rb' LoadError: cannot load such file -- ruby/app_acad_mini_curriculum/debugging/primes.rb from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require' – Ryan Bonhardt May 07 '13 at 19:32
  • I should have mentioned that in regards to putting `binding.pry` at the end of your script, you would run `primes.rb` instead of going into pry and trying to require it. – Charles Caldwell May 07 '13 at 19:38
  • As to why your require inside pry isn't working, it is for the same reason it doesn't work inside irb. You would need to pass it the full path to the file (which would start with `/` in Linux or MacOS and `C:/` in Windows). However, if you run `binding.pry` from inside your script, `require_relative 'another_script.rb'` should work. – Charles Caldwell May 07 '13 at 19:41
  • THANK YOU! I finally got it to work with last comment. Thank you – Ryan Bonhardt May 07 '13 at 19:45
  • Your welcome. Using Pry can have a slight learning curve but once you grasp it, it is magic. =) – Charles Caldwell May 07 '13 at 19:49
0

unlike ruby 1.8, you can't require a file that is in the same folder, because the current folder is not on the load path any longer.

To emulate the behavior of ruby 1.8, you could try

$LOAD_PATH << "."
require 'primes.rb'

However, the correct way to do in ruby 1.9, as @CharlesCaldwell pointed, is using relative_require.

Here is a good discussion of the best way to deal with this.

note that relative_require does not work in irb. You can check the motive on @CharlesCaldwell answer.

But looking in your task question, you should not use irb, you should use pry:

We're going to use two gems. One is called Pry, which is a replacement for irb. You'll have to gem install pry. It's not essential for debugging that you use Pry, but it will make life nicer.

Here is an example using relative require:

[fotanus@thing ~]$ cat primes.rb 
# primes.rb

def prime?(num)
  (1..num).each do |i|
    if (num % i) == 0
      return false
    end
  end
end

def primes(num_primes)
  ps = []
  num = 1
  while ps.count < num_primes
    primes << num if prime?(num)
  end
end

if __FILE__ == $PROGRAM_NAME
  puts primes(100)
end
[fotanus@thing ~]$ cat a.rb 
require_relative 'primes.rb'
[fotanus@thing ~]$ ruby a.rb 
Community
  • 1
  • 1
fotanus
  • 19,618
  • 13
  • 77
  • 111
  • I updated my results above when I try this. Any other suggestions? Thanks – Ryan Bonhardt May 07 '13 at 19:06
  • @RyanBonhardt updated my answer - by any change, was you using irb? – fotanus May 07 '13 at 19:15
  • yes I was. really all I'm trying to do is complete this exercise: https://github.com/appacademy/prep-work/blob/master/mini-curriculum/debugging/debugger.md should I just run it in irb instead? – Ryan Bonhardt May 07 '13 at 19:21
  • @RyanBonhardt by your task description, you should not use `irb`, you should use `pry` – fotanus May 07 '13 at 19:29
  • thank you. I'm obviously new to this and wasting hours on small things like this. I've updated my error messages above when I use pry – Ryan Bonhardt May 07 '13 at 19:36