File resource location?

J

Jim Crossley

I have a Java application that I wish to port to Ruby. One of its
features is to convert a string of digits to a raw audio stream of
DTMF tones.

So the Java jar file includes raw audio files for each digit. These
are loaded as resources from the classpath.

What's the *right* way to do that in Ruby? Where do people typically
package resources for their applications?

Thanks,
Jim
 
R

Robert Klemme

Jim Crossley said:
I have a Java application that I wish to port to Ruby. One of its
features is to convert a string of digits to a raw audio stream of
DTMF tones.

So the Java jar file includes raw audio files for each digit. These
are loaded as resources from the classpath.

What's the *right* way to do that in Ruby? Where do people typically
package resources for their applications?

Never had that problem, but one way would be to place them beside the module
..rb file or in a subdirectory and load them via the module file's name.

Regards

robert
 
A

Austin Ziegler

Never had that problem, but one way would be to place them beside
the module ..rb file or in a subdirectory and load them via the
module file's name.

I've solved it a couple of ways. The first was to basically do:

% irb --noprompt
Dir["icons/*jpg"].each do |file|
File.open("#{file}.rb", "wb") do |wfile|
data = nil
File.open("#{file}", "rb") { |rfile| data = rfile.read }
wfile.write "#{file.tr('/.', '_').upcase} = #{data.inspect}"
end
end

The bad news, of course, is that this makes the file MUCH larger. If
you wanted, you could BASE64 encode the files and then decode them
on reading. However, the trick here is that you will get a valid
Ruby source file for your resource, and it can be found without any
additional work.

Alternatively, you can do what I'm doing in PDF::Writer, which is to
look for resource files in a predictable location, or in a specified
location. I do this with .afm files and now the appropriate .ttf
files.

-austin
 

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
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top