Using external libraries in Ruby - ruby

I have compiled a library(GDAL) written in C/C++ with Ruby bindings.
After a ran make install it was installed under
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0/
There is a directory there called "gdal" with 4 files inside: gdal.so, gdalconst.so, ogr.so, osr.so
What I want now is to use these libraries in my project. The problem is that when I try to referense these files
require 'gdal/gdal'
I get
LoadError: no such file to load --
gdal/gdal from
:29:in
require' from
<internal:lib/rubygems/custom_require>:29:in
require' from (irb):1 from
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/bin/irb:16:in
`'
When I run ruby -e 'puts $:' I get following output:
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1
/Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0
Any ideas why it doesn't work for me?

After make install on OS X you should have a .bundle file in your path /Users/igor/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0.
If you want to use it then, you just have to require 'gdal' and it should fine.

It looks like you are not using a extconf.rb file to help you generate your makefile.
You might want to read this tutorial and check Rice if you want to write a C++ extension. (Ruby is written in C and you need to expose a C API, Rice makes that easier).

Thank you guys for your suggestions. I've finally figured out the problem. It seems like the fact that I was using rvm to manage ruby versions was causing the problem. As soon as I switched to system default(rvm use system) everything worked out well.

Related

Why did my minitest/pride alias break?

Silly, cosmetic question for everyone.
I take pride in my testing. That is, I like to use minitest/pride. Unfortunately, not everyone on my team is a fan, so I have activated it in the past using the command line instead of modifying the code by setting an alias to this:
RUBYOPT=-rminitest/pride bex rake`
(bex is an alias to bundle exec, and the default rake task runs our tests.)
Recently, we updated the ruby version of our project from 1.9.3 to 2.2.0. This has broken my alias, and now when I try to run it, I get this error instead:
/Users/cf5455/.rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- minitest/pride (LoadError)
from /Users/cf5455/.rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
My best guess as to why this is happening is that MiniTest was changed since our last Ruby version and pride has been renamed or moved, but I have not been successfully able to Google its new name or location.
Does someone know a way that I can fix my arguments to get pride working again?
Duplicated your error by switching to ruby 2.3.0. I just ran the tests before installing any gems.
I managed to get the tests running with your alias by simply doing a gem minitest install. Not sure if your problem is caused by the same reason since I'm using rbenv for managing my rubies and gems but it does feel like you are having a package management issue. Hope this helps.

How to run wukong examples?

Wukong looks like a pretty nice Ruby way to get into Hadoop through the streaming interface. However, after looking over the examples at the git page:
https://github.com/infochimps-labs/wukong/tree/master/examples
I have no idea how to actually run any of these and generate output.
EDIT: I tried running the following example:
$ ./word_count.rb --run=local README.txt .
This generates an error:
/Users/evanzamir/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- wukong/script (LoadError)
from /Users/evanzamir/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from ./word_count.rb:2:in `<main>'
It seems to be having an issue with the line require 'wukong/script', but I'm not sure how to fix it.

compass failing to load singularitygs gem

Trying to get a new project set up using Singularity.gs but I keep running into a snag with Compass.
First I used Terminal to install the necessary Ruby Gems (sass, compass, singularitygs, respectively).
Next I created a new project using the command:
compass create project1 -r singularitygs --using singularitygs
In the directory of my choosing.
At this point, in an effort to make life easier I've also attempted to use offerings like Scout (http://mhs.github.io/scout-app/) and/or Compass.app (http://compass.handlino.com/) so I can take advantage of a more user-friendly GUI.
I'm running into problems using either of these apps however, so I'm assuming it has something to do either with my Ruby gem install of singularitygs or with the creation of a new project.
Specifically, once I have a target directory and folder structure in place, I'll attempt to "watch" the sass folder and get an error that looks like:
(Compass)
no such file to load -- singularitygs
org/jruby/RubyKernel.java:1062:in `require'
file:/Applications/compass.app/Contents/Resources/Java/compass-app.jar!/require_patch.rb:6:in `require'
or
(Scout)
LoadError on line 1038 of org/jruby/RubyKernel.java: no such file to load -- singularitygs
/Applications/Scout.app/Contents/Resources/vendor/gems/gems/compass
0.12.2/lib/compass/configuration/data.rb:161:in `require'
Strangely enough, I also get a very similar error message in Scout if I try to watch a Sass folder inside of a project that's using Foundation (having also installed the zurb-foundation ruby gem previously)
I am SUPER new to getting Sass / Compass set up so I'm definitely feeling a bit out of my element. I know this is probably not the easiest thing to diagnose from my description, but I'm not sure what I'm doing wrong. Hopefully I've managed to describe my issue clearly enough. Bear with me if any of this appears vague or incomplete, definitely not my intention.
Thanks so much.
Apps like Scout and Compass.app come bundled with their own versions of Ruby so even though you installed the gems on your computer they are not installed when compiling from those apps.
Here is how to make Compass.app use the version of Ruby on your computer. Open preferences and check “Custom”. Note your path may be different:

Fixing "custom_require.rb:36:in `require': no such file to load" errors

I understand that after Ruby 1.9.2, '.' is no longer in your path for security reasons. This seems to be a problem when using certain gems (ones not updated to 1.9 I imagine?), a problem that throws errors like
$HOME/.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 -- rubylog (LoadError)
I've seen and fixed this problem once, by (perhaps naively) changing some requires into require_relatives: https://github.com/mathpunk/MongoDB_Koans/commit/e2f7898347d328450ec121d22f701508f389cc53
Now I'd like to use rubylog, and I'm getting the custom_require error, so I tried the same trick:
https://github.com/mathpunk/rubylog/commit/995e13dccc6a197d280d0783f3fb7fe50deabd02
but this time, I'm just getting the same error. What else can I try?
ETA: All this time, I've been using sudo gem install blah to install gems, and for some reason, for rubylog it's gem install rubylog that does it. (Something to do with RVM?) So now everything works. Thank you.
Your code fails at require 'rubylog' - so it can't find rubylog.rb itself. So just add dir containing rubylog.rb to load path - something like $: << 'rubylog' might help.
Just add the library directory to your LOAD_PATH:
$LOAD_PATH.unshift(File.dirname(__FILE__))
See Understanding Ruby's load paths\
Edit: I assumed you had a rubylog directory wherever your script was running from. If your script can't find rubylog then you need to add that location to your load path:
$LOAD_PATH.unshift('/path/to/rubylog')
Are you sure you have the rubylog libraries? gem install rubylog

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

Resources