SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard xyuri's Avatar
    Join Date
    Jul 2002
    Location
    Brisbane
    Posts
    1,487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile 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 work If required later I will post some of it.

    If someone knows what might be going on here I would be very grateful Thank 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.

  2. #2
    SitePoint Evangelist
    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'
    The path to chronic needs to be relative to the script file.

    Or you could use the absolute path:
    Code:
    require '/absolute/path/to/chronic'
    Obviously this needs to be done with care or you are going to run into difficulties if you move the script to another system.

    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'
    That effectively makes all the objects and plug-ins available to your Rails application also available to the script. It also sets the environment to 'production' so maintenance tasks run against the correct data.

  3. #3
    SitePoint Wizard xyuri's Avatar
    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?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,145
    Mentioned
    190 Post(s)
    Tagged
    2 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
    I imagine OS X has something similar?

  5. #5
    SitePoint Evangelist
    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
    One within a *.rb file and one directly within irb. I get the same result - that is the same working directory. Therefore, I think my first post might have been a little misleading. It's not the working directory path that is important, but rather what paths are available to the current process.

    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.
    1. 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.
    2. 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}
      That may well be a simpler path than the previous ones you tried and is probably more flexible in that it is more likely to work on other systems.

  6. #6
    SitePoint Evangelist
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •