SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Oct 8, 2008, 22:18 #1
- Join Date
- Jul 2002
- Location
- Brisbane
- Posts
- 1,487
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
library works in irb but not in script
I need to parse random datetime formats, which "chronic" seems to help with. so ...
sudo gem install chronic
Ruby finds and installs without any problems. I try it out in irb without any problems, but when i require 'chronic' in my ruby script (just a file script) it can not find the library. What is going on here?
I must appologise for not posting exact error messages or code, since I am at workIf required later I will post some of it.
If someone knows what might be going on here I would be very gratefulThank you.
It might be worth adding that this is occuring on my OS X machine. I have just tried it on windows and it seems to be working fine. Perhaps the OS X version is not installing correctly?Last edited by xyuri; Oct 8, 2008 at 22:37. Reason: Additional, possibly helpful information.
-
Oct 9, 2008, 02:23 #2
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I expect the problem is the path to the "chronic" code relative to script file. When you run irb paths are relative to the irb code location. When you run a script, paths are relative to the script file.
Try using this form of the require statement:
Code:require File.dirname(__FILE__) + '/path/to/chronic'
Or you could use the absolute path:
Code:require '/absolute/path/to/chronic'
If this is part of a Rails application I find this works well:
Put your maintenance scripts in the root of your application and then add this to the start of the script:
Code:ENV['RAILS_ENV'] = 'production' require 'config/environment.rb'
-
Oct 13, 2008, 21:26 #3
- Join Date
- Jul 2002
- Location
- Brisbane
- Posts
- 1,487
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This does seem to work, although on OS X the paths are long and look ugly. I don't really want to do this since it should not be necessary! Is there a way to check where ruby is looking for these files?
-
Oct 14, 2008, 17:07 #4
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
I don't know about OS X, but for Windows I had to add the path to the autoexec.bat file and reboot.
Code:SET PATH=%PATH%;c:\RUBY\BIN;C:\mysql\bin
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Oct 15, 2008, 03:10 #5
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've had a little play. I've run this command in two ways:
Code:puts Dir.getwd
If you look at irb.rb in the ruby folders you can see that it loads a lot up on start. Somewhere in there, it is loading something that is providing access to the plug-ins. On a quick look, I couldn't see where that is happening.
However, I could 'require irb' within the .rb file code. That suggest two possible paths to you.
- The quick and dirt option: I think if you add 'require irb' to your script, things available in IRB will be available to your script.
- If 'require irb' works then you know the path to the irb.rb file is accessible without specifying a path and therefore the folder containing that file is accessible without a full path (on my system that's 'C:\ruby\lib\ruby\1.8'). Therefore, you may be able to specify a path to chronic relative to that directory. On my system I think that would be:
Code:../gems/1.8/gems/{chronic_folder}
-
Oct 21, 2008, 08:18 #6
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Another thing to try: add this line to the start of the script file
Code:require 'rubygems'
Bookmarks