PDA

View Full Version : [SOLVED] Ruby Loadpath



nova47
October 23rd, 2010, 02:26 AM
I'm attempting to require a class in a separate class. However, I have found that Ruby requires me to hardcode the address of the file I'm requiring. EG: I have to type require '/home/grant/...blank.rb' rather than just blank.rb to actually get the file to be properly included in the code. The issue is that this is a school project that will be turned into a grader who's path will obviously not be identical to mine. How do I make it so the class path does not have to be hardcoded in order to work?

Code:



require 'F:\Programming\eclipseWorkspace\CSE_655\file_hand ler.rb'


class Main

print "Enter file path: "

#Get the file name from the user and open the file.
file = FileHandler.new(gets)

end


FileHandler Class:



class FileHandler

def initialize(fileToParse)
@fileToParse = fileToParse
end


end

dv3500ea
October 23rd, 2010, 10:14 AM
You can add paths to the require search path like this:



$:<< ENV['HOME'] #add user's home directory to search paths
$:<< "#{ENV['HOME'}/another_folder" #add users ~/another_folder to search paths

nova47
October 23rd, 2010, 02:16 PM
You'll have to bare with me, I'm really new to Ruby, but how do I make it work just like Java where all I do is make sure the user keeps everything in the same folder and it will still run? The grader isn't going to want to have to manually input the path.

dv3500ea
October 23rd, 2010, 02:29 PM
If everything is in one folder, you can use relative paths. You can replace
ENV['HOME'] with
Dir.pwd in the example above.

gmargo
October 23rd, 2010, 02:42 PM
How do I make it so the class path does not have to be hardcoded in order to work?

There are a number of environment variables that Ruby is supposed to use; RUBYLIB is probably what you're looking for.

http://www.ruby-doc.org/docs/ProgrammingRuby/html/rubyworld.html#S3

nova47
November 2nd, 2010, 05:30 PM
Is there no way to just say the current working path? What if he doesn't put it in the folder that I add to path etc? Basically I want it to do exactly what Java does. I don't want to have to hardcode the path in any way. I just want to say keep it all in the same folder and run it.

dv3500ea
November 2nd, 2010, 05:58 PM
You can get the directory the file you are running is in with:



File.dirname(__FILE__)


You can require relative paths from there:



require "#{File.dirname(__FILE__)}/../../lib/something.rb"

That example would require the ruby file that is called 'something.rb' and in the 'lib' folder of the folder 2 folders above the folder that the ruby program is in.

nova47
November 2nd, 2010, 06:17 PM
Where is the .basedir method defined? When I tried to put it in my code I got the error



F:\Programming\655\655\main.rb:11
F:/devtools/ruby187/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/lib/ruby-debug.rb:101:in `debug_load'
F:/devtools/ruby187/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/lib/ruby-debug.rb:101:in `debug_program'
F:/devtools/ruby187/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.5/bin/rdebug-ide:82
F:/devtools/ruby187/bin/rdebug-ide:19:in `load'
F:/devtools/ruby187/bin/rdebug-ide:19
Uncaught exception: undefined method `basedir' for File:Class


I went and looked up the File class at http://ruby-doc.org/docs/ProgrammingRuby/ and I don't see it there either. There's a basename method though. That's the closest I found. But it returns the last component of the filename given in fileName which would just be the name of the class.

dv3500ea
November 2nd, 2010, 06:20 PM
Sorry, it is
dirname (I was mixing it up with basename that gives you just the file name without the full directory path.)

nova47
November 2nd, 2010, 06:29 PM
I just figured it out. Thanks dv3500ea. There wasn't a basedir method but I just figured out what you meant by relative paths. I just used Dir.pwd. When you originally posted I didn't know where to put it. (I hadn't yet discovered the pragmatic programmers guide so I didn't know what Dir.pwd did. :-D) I looked up the Dir class and discovered what it did and how to use it lol

nova47
November 2nd, 2010, 06:34 PM
Next question how do you get it into the requires statement?

I tried:

require 'Dir.pwd\file_handler.rb'

require "Dir.pwd\file_handler.rb"

require Dir.pwd\file_handler.rb

None of which it liked :-(

dv3500ea
November 2nd, 2010, 06:37 PM
String interpolation:



require "#{Dir.pwd}/file_handler.rb"

nova47
November 2nd, 2010, 06:39 PM
Ya I feel a bit foolish (again) I just read your old posts and came up with:

$:<< Dir.pwd

require 'file_handler.rb'
require 'tokenizer.rb'

And got it to work. I'll use your code to make it a little more clean. Thanks for all the help. It hasn't been an easy switch from Java, C++/C# to something like Ruby lol. Just out of curiosity what function do the brackets add?