Calling a Commandline tool using Ruby

102 Views Asked by At

The commandline tool I trying to call basically converts a file to another format.

I'm following along using this [tutorial][1] but am running into trouble. I keep getting invalid multibyte char (UTF-8) (SyntaxError)

I'm just dumping my commandline tool (made using C) in the bin and am trying to get it to work. Could someone shed some light on how I can get this working?

2

There are 2 best solutions below

6
patm On BEST ANSWER

The error you are getting is related to UTF-8 encoding, which is the default encoding in ruby-2.x so I'm assuming you're using 1.8 or 1.9 and you should consider upgrading.

Try adding the following lines to the top of your script:

#!/bin/env ruby
# encoding: utf-8

Let us know if it works and have a look at this thread for more.

EDIT: Now I understood what you want to do. Take a look here for an example file which sits at the bin/ directory. You can put your executable file to another directory like data/ for example, and call through ruby:

#!/usr/bin/env ruby

prog = File.expand_path('../data/myprog', __FILE__)
ARGV.each do|a|
    system("#{prog} #{a}") # will return 'true' if run successfully
end

Okay try now. This should work with multiple files also, like

prog file1.doc file2.doc ...

1
Малъ Скрылевъ On

Please, read the document, and the document on how to make C soft compatible with ruby.

Some notes:

  1. Place all files in proper folders for the gem;
  2. Make the gem extconf compatible;
  3. Don't put compiled binaries into GitHub source (in some restricted cases the precompiled binaries for Windows OS could be stored);
  4. If you don't write an OpenSource project, don't publish it into GitHub, instead of it use BitBucket, and localhost (non Rubygems.org) to store the gem.