How to change working directory in JRuby? - ruby

I have to run my Ruby script from path that is higher than a script. My Ruby file is in folder lib. I start it in console:
jruby --1.9 -Clib main.rb
but it doesn't work correctly. It changes Dir.pwd, but require doesn't see it and another library DataMapper doesn't see it too.
I know I can add path to be seen by require by -Ilib, but it doesn't fix DataMapper issue and it is ugly I think.

require loads a file from the $LOAD_PATH. If the directory the file you want to load is in is not on the $LOAD_PATH, then require won't find it. If you want to load a file not from the $LOAD_PATH but relative to the position of the currently executing file, you need to use require_relative.

Assuming this is your folder structure
app/other/some_class.rb
app/lib/main.rb
If you navigate to the lib folder
cd app/lib
Then run your main.rb script
jruby main.rb
You can refer to the some_class.rb file in your main.rb script with this line
require "../other/some_class.rb"

Related

LoadError occurs when directly running Ruby source code in existing projects

I'm new to Ruby/JRuby and has been disturbed by the error "LoadError: no such file to load" for many weeks, when I try to directly run the Ruby source code of certain projects.
I downloaded the source code of many Ruby projects from GitHub. Yes only the source code, I didn't install them because my task is more on analyzing the code itself.
Let's take an example, say the project "rqrcode" has the following (simplified) structure:
rqrcode
lib (folder)
rqrcode (folder)
core_ext (folder, with some files inside)
core_ext.rb
qrcode (folder, with some files inside)
qrcode.rb
rqrcode.rb
test (folder)
data.rb
test_rqrcode.rb
So if I run "jruby test_rqrcode.rb" inside the test folder, it throws LoadError at this line inside the file:
require_relative "../lib/rqrcode"
And it also throws LoadError at here, the rqrcode.rb file in lib folder:
require "rqrcode/core_ext"
The error message is
LoadError: no such file to load -- rqrcode/core_ext
require at org/jruby/RubyKernel.java:1054
require at /Users/x5lai/.rvm/rubies/jruby-1.7.4/lib/ruby/shared/rubygems/custom_require.rb:36
(root) at /Users/x5lai/Downloads/rqrcode-master/lib/rqrcode.rb:12
require at org/jruby/RubyKernel.java:1054
(root) at /Users/x5lai/.rvm/rubies/jruby-1.7.4/lib/ruby/shared/rubygems/custom_require.rb:1
require at /Users/x5lai/.rvm/rubies/jruby-1.7.4/lib/ruby/shared/rubygems/custom_require.rb:36
(root) at test_rqrcode.rb:12
I really don't understand why it says it cannot find "rqrcode/core_ext" because that folder does exist there!
Such error doesn't always occur. Sometimes, when I download the source code of other Ruby projects which have similar structure as above, it runs successfully with all the "require", "require_relative" statements.
My friend says it is a Ruby default load path problem. I therefore went to look at what's inside my Ruby load path. It's full of many Ruby files. But, those Ruby projects that run successfully do not have their Ruby files in these load path as well (and they do not use ".unshift" to modify their load path inside their code). So I don't think this is the cause of those failing projects.
Hope there's someone who could clarify my doubts. Maybe it's because of my JRuby configuration? I'm using a Mac. My JRuby version is 1.7.4.
Firstly, ruby load path doesn't include current directory.
You can verify this by running jruby -e "$:" in cmd.
Secondly, when you do require_relative "../lib/rqrcode" in test_rqrcode.rb, you are saying "please find the file at a path relative to myself". Okay, it can find rqrcode.rb right away. However, rqrcode.rb doesn't know where to find its own required files, so it goes to global load path, which is the $:. Since $: doesn't include the lib folder, it cannot find any file residing inside its lib folder, thus return a exception.
Knowing this, you should add local lib directory to the load path in your main script, so every subsequent file will use the same load path environment.
$:.unshift "path_to_the_folder_need_to_include"
On the command line, you can add a folder to the $LOAD_PATH by using the -I switch. For example:
ruby -I lib test/test_qrcode.rb
It is common for ruby projects to add their lib folder to the $LOAD_PATH on their test setup, typically on a file called test_helper.rb or spec_helper.rb (depending on the framework).

Can't get RSpec to work -- 'require': cannot load such file

I just spent three days of my life banging my head against the wall trying to figure out why a simple 'rake' would not pass my spec file.
If this happens to you: Do not have a space in any folder path!. Seriously. In fact do not have a space in anything you name from here on out.
Here is my console output:
(in /Users/*****/Desktop/Learning Ruby/learn_ruby)
$ rake
/Users/*******/Desktop/Learning Ruby/learn_ruby/00_hello/hello_spec.rb:116:
in `require': cannot load such file -- hello (LoadError)
The failure is caused by the line: require "hello"
This line tells Ruby that it needs to search the load path for a file named hello.rb. However, when it looks at the load path, it can't find that file. You should either remove that line and define your code directly in the spec file, or create a hello.rb file.
Newer versions of RSpec (2.11+ I believe) automatically add subdirectory lib to the load path. Based on your Rakefile it seems you are also loading the current lab directory and the subdirectory solution.
I'm guessing you're expected to put your solution in solution/hello.rb.
What worked for me was changing the require statement to a require_relative
I am using windows and an IDE

ruby require not working

I'm new to ruby, but I'm working on my first ruby program. It currently has two files, one is a library of functions (xgync.rb stored in lib) the other is the executable xgync stored in 'bin'. (Project visible here https://bitbucket.org/jeffreycwitt/xgync/src) I've also created a symlink to my /usr/local/bin/xgync so that I can write the command xgync {arguments} from anywhere in the terminal.
The problem seems to be that bin/xgync depends on the library lib/xgync.rb. I've written this dependency in bin/xgync as follows:
$:.unshift(File.dirname(__FILE__) + '/../lib')
require "xgync"
However, i keep getting the following error:
/Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- xgync (LoadError)
from /Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/bin/xgync:4:in `<main>'
can you see anything wrong with what I've written? Could the symlink be somehow messing things up?
Thanks for your help :)
When using ruby 1.9.x you don't usually alter the path with the $:.unshift when requiring other files in your project.
Instead the best practice is to use require_relative instead.
require_relative '../lib/xgync.rb'
require_relative requires files relative to the file you are currently editing.
But the error you experience appears, because you require a file, which does not exist:
bin/xgync
lib/xgync.rb
These are the files in your project according to your question, and the code-excerpt is from bin/xgync you extended the path to look for files in lib/ but you try to require 'xgync' which is a file, that is not present in lib/, so if you wanted to use this method (instead of require_relative you would have to use require 'xgync.rb'.

Why do I have to interpolate Dir.pwd when using require or autoload in Ruby?

This seems to be a Ruby 1.9 problem for me, but anytime I try to require or autoload source with something like require "lib/mylibrary" Ruby fails with a "No such file to load" error. I always have to interpolate Dir.pwd thusly: require "#{Dir.pwd}/lib/mylibrary"
I see source everywhere that doesn't need to look up the present working directory to include source files. What am I missing?
The $LOAD_PATH variable determines the places that Ruby will check for files to load. As of Ruby 1.9, the current directory is not in the load path by default, but you can use the require_relative method to require files relative to the current working directory.
See this question for more details.

Ruby - adding a directory to $LOAD_PATH - what does it do?

This is actually a question about this question: Adding a directory to $LOAD_PATH (Ruby)
What happens when you add a directory to the $LOAD_PATH? Is it like adding a script file to the execution context as in JavaScript? (You can access global methods/objects in another files) If not, how do I call the methods of other ruby files in the current directory?
When you add the /Users/you/scripts/ruby directory to the load path, you can use:
require 'example'
instead of:
require '/Users/you/scripts/ruby/example.rb'
Think of the $LOAD_PATH as to being similar to the PATH variable on a operating system. If certain directories are in the LOAD_PATH, you can just write require "some_module".
It's also the reason for being able to require files from the current directory.
By default, the LOAD_PATH no longer includes the current directory . having been removed in Ruby 1.9.2.

Resources