packaging hybrid extension?

R

Rick DeNatale

I'm writing something which is partially written in ruby, but uses an
extension which adds methods to some of the classes in the ruby part.

Right now I've got a directory which contains both the ruby code and
the so containing the extensions

$ls
mycode.rb mycode_prims.so

in mycode.rb I've got something like:

class Mycode
def foo
end
end

# link in the primitives, mycode_prims includes various calls to
# rb_define_method to add methods to the Mycode class.
require 'mycode_prims'

Now this works if the directory containing both parts is the current
directory, but not otherwise. Actually I guess it would work as long
as the directory was on the load path, but I'm concerned about the
possibility of loading the wrong mycode_prims.so.

Is there a way to get the directory containing the current source
file? __FILE__ only gives the file name, but not the path.

What is the right way to do this? I'd like to know how to approach
this either with the code packaged as a gem or as 'source'.
 
A

ara.t.howard

I'm writing something which is partially written in ruby, but uses an
extension which adds methods to some of the classes in the ruby part.

Right now I've got a directory which contains both the ruby code and
the so containing the extensions

$ls
mycode.rb mycode_prims.so

in mycode.rb I've got something like:

class Mycode
def foo
end
end

# link in the primitives, mycode_prims includes various calls to
# rb_define_method to add methods to the Mycode class.
require 'mycode_prims'

Now this works if the directory containing both parts is the current
directory, but not otherwise. Actually I guess it would work as long
as the directory was on the load path, but I'm concerned about the
possibility of loading the wrong mycode_prims.so.

Is there a way to get the directory containing the current source
file? __FILE__ only gives the file name, but not the path.

What is the right way to do this? I'd like to know how to approach
this either with the code packaged as a gem or as 'source'.

one idea is to do something like this

in foo.rb

require 'rbconfig'

arch = Config::CONFIG["arch"]
dlext = Config::CONFIG["DLEXT"]

this = File.dirname __FILE__
archdir = File.join this, arch

lib = File.join archdir, "foo.#{ dlext }"
require lib

then you layout dirs like so

lib/foo.rb
lib/i686-linux/foo.so
lib/mswin/foo.dll

and you can distribute one gem, one tarball, etc. you also could just dump
the *.so right into the libdir and do a


lib = File.join(File.dirname(__FILE__), "foo.#{ dlext }")
require lib

for example. the key point is to bury the native lib close to the *.rb file
and do a relative require.

hope that made sense...

-a
 
R

Rick DeNatale

this = File.dirname __FILE__
archdir = File.join this, arch

lib = File.join archdir, "foo.#{ dlext }"
require lib

The problem is that __FILE__ only has the file name, not the path, so

File.dirname(__FILE__) => '.'
 
A

ara.t.howard

The problem is that __FILE__ only has the file name, not the path, so

File.dirname(__FILE__) => '.'

na. it's always relative to the current directory, so you just need to expand
it (i forgot this part - sorry)


mussel:~ > cat /tmp/b.rb
puts File.dirname((__FILE__))

mussel:~ > ruby /tmp/b.rb
/tmp



mussel:~ > cat /tmp/a.rb
puts File.dirname(File.expand_path(__FILE__))

mussel:~ > ruby /tmp/b.rb
/tmp

mussel:~ > ruby -I/tmp -r a.rb -e nil
/tmp


i use this technique all over the place for relative requires:

http://codeforpeople.com/lib/ruby/rq/rq-2.3.4/lib/rq-2.3.4.rb


sorry for confusion.

cheers.

-a
 
R

Rick DeNatale

na. it's always relative to the current directory, so you just need to expand
it (i forgot this part - sorry)


mussel:~ > cat /tmp/b.rb
puts File.dirname((__FILE__))

mussel:~ > ruby /tmp/b.rb
/tmp



mussel:~ > cat /tmp/a.rb
puts File.dirname(File.expand_path(__FILE__))

mussel:~ > ruby /tmp/b.rb
/tmp

mussel:~ > ruby -I/tmp -r a.rb -e nil
/tmp


i use this technique all over the place for relative requires:

http://codeforpeople.com/lib/ruby/rq/rq-2.3.4/lib/rq-2.3.4.rb


sorry for confusion.

Excellent! Thanks Ara.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top