Compile ruby script with dependencies on other classes - ruby

have a problem with compressing my script.
I have a main.rb and some classes in subfolders like Subfolder/Class.rb
In my main.rb, I have the Classes declared like that:
require './Subfolder/Class.rb'
When I just run my main script, it works. Also my exe works, when it is in the same place as the main.rb.
But when I put the exe somewhere else I get this error:
C:/Users/MLEING~1/AppData/Local/Temp/ocr53C2.tmp/lib/ruby/site_ruby/1.9.1/rubyge
ms/custom_require.rb:36:in `require': cannot load such file -- ./Parsing/Calibra
tionState (LoadError) from C:/Users/MLEING~1/AppData/Local/Temp/ocr53C2.tmp/lib/ruby/site_ruby
/1.9.1/rubygems/custom_require.rb:36:in `require'
from C:/Users/MLEING~1/AppData/Local/Temp/ocr53C2.tmp/src/main.rb:9:in `
<main>'
Can I somehow put the dependencies into my exe?
I also tried to include them like that:
ocra main.rb Subfolder/*.rb
But it doesn't help.

Have you tried making a ruby gem out of your project? http://guides.rubygems.org/make-your-own-gem/
Gems define their own dependencies.

Your require is using a relative path from the current directory (which you can see because it starts with "./"
Instead, try:
require 'Subfolder/Class.rb'
And make sure $LOAD_PATH includes the location where all of your ruby code is unpacked (which you can look at by examining $0 (or figure out the full path from $0 and require the .rb with a full path)

Related

How do I make ruby ocra work with multiple source file?

I'm using ocra to convert my rb scripts to exe, but if it has multiple sources, the exe will show the LoadError complaining it can't find the other source files.
For example, in my main.rb:
require_relative 'lib/user'
# blabla bla
after I packing my main it with either ocra main.rb ocra main.rb ./lib/user.rb, then run the main.exe elsewhere and it says cannot load such file -- lib/user (LoadError)
How do I make it work with multiple sources?
OK so I missed this from the manual...
OCRA does not set up the include path. Use $:.unshift File.dirname($0)
at the start of your script if you need to 'require' additional source
files from the same directory as your main script.
added $:.unshift File.dirname($0) at the start of my entry script, also changed my require './somescript' to require 'somescript' then it works

Ruby: require works in gem, fails when running from source

Try creating a gem based on bundler's official guide on developing a Ruby gem.
Running bundle gem foodie will create a structure and generate files in the lib directory:
foodie
version.rb
foodie.rb
foodie.rb reads
require "foodie/version"
module Foodie
# Your code goes here...
end
Running ruby lib/foodie.rb (or also from different directories) will result in
C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- foodie/versio
n (LoadError)
from C:/Ruby23-x64/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from foodie.rb:1:in `<main>'
On the other hand installing the gem via rake install and then requiring the gem works just fine.
It works from source if require "foodie/version" is changed to require_relative "foodie/version" in foodie.rb. As I understand
require works based on modules
require_relative works based on directory structure
To me the latter looks like a hack. It'd no longer make sense to structure your code via modules as it wouldn't be enforced (maybe it'd still make sense but you could make mistakes and never notice).
My questions are:
Is it possible to test a gem from source without installing it while following the bundler convention (using require instead of require_relative)?
Why does the gem work after installed?
Is there any best practice for the usage of require, require_relative, modules, files and general structure?
Thank you.
You need to add your lib dir to Ruby’s load path. The load path is a list of directories that Ruby searches for files in when you call require. Rubygems also manages the load path when you are using gems, which is why your code works when installed as a gem.
You say “as I understand ... require works based on modules”, this is not correct. require works with files, it’s just convention that a class or module is defined in a file with a matching name, e.g. MyModule might be in my_module.rb.
There are a few ways to add a dir to the load path. From the command line you can use the -I option:
$ ruby -I lib lib/foodie.rb
If you wanted to avoid typing -I lib you could use the RUBYLIB environment variable. Ruby adds the contents of this to the load path:
$ export RUBYLIB=lib
$ ruby lib/foodie.rb
(On Windows I think you will need to use set rather than export.)
You can also manipulate the load path from withing the program itself. It is stored in the global variable $LOAD_PATH, aliased as :$. This is how Rubygems and Bundler manage your gems.

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'.

How to change working directory in JRuby?

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"

Eclipse Ruby Development Tools "require" fails

I'm using Eclipse with RDT to do some Ruby programming. I'm trying to include a file in another, but require fails. Both files are in the same directory.
The folder hierarchy is set up like this:
Project > src > folder > a.rb b.rb
If I try to require b.rb in a.rb I would use this:
require 'b.rb'
But I get the following error message:
src/folder/a.rb:1:in `require': no such file to load -- b.rb (LoadError)
from src/folder/a.rb:1
If, however, I specify the full path it works:
require '/home/peter/workspace/project/src/folder/b.rb'
But, obviously, using the full path is a bit stupid.
How can I fix this?
Like evoked here, if the ruby editor uses a ProcessBuilder to call ruby, the working directory is whatever it was when the JVM was started.
A good test would be start eclipse from the "Project > src > folder" directory to see if the relative path is seen then.

Resources