64
64

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WindowsでRubyコードをexe化するときのメモ

Posted at

Textデータを変換するツールをWindows上で動かす必要がでてきたので、
Rubyで簡単に書いて、なおかつexeとして動かしたいと思って調べました。
(rubyの入っていない環境でも動かしたかったので)

ぴったしのサイトが見つかり、簡単にできそうなことがわかりました。

RubyInstallerのRuby2.0でRubyInlineとOcraを使ってexe化する

しかし、最新版の組み合わせではexe化するときにエラーが発生したため、動いた組み合わせをメモとして残しておきます。

=== Adding user-supplied source files
C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:1121:in `initialize':
 No such file or directory @ rb_sysopen - C:/Users/ogi/thread.rb (Errno::ENOENT)

動いたバージョン組み合わせ

動かなかったバージョン組み合わせ

  • Ruby Installer Ruby 2.1.5
  • DevKit(For use with Ruby 2.0 and 2.1)
  • RubyInline 3.12.3
  • Ocra 1.3.3

手順

  • Ruby Installerでrubyをインストール

  • DevKitを適当な場所に展開(C:¥DevKitなど)

  • スタートメニューから"Ruby コマンドプロンプトを開く"を起動し、DevKitの場所に移動 cd C:¥DevKit

  • DevKit初期化 ruby dk.rb init

  • DevKitとRubyを紐付け ruby dk.rb install

    Rubyの配置場所のPathにスペースが入っているとダメみたいです。
    詳しくはDevelopment Kitを参照。

  • コードを用意し(foo.rb)、コードの先頭に次の一行を追加

    ENV['INLINEDIR'] = File.dirname(File.expand_path(__FILE__))

foo.rb
ENV['INLINEDIR'] = File.dirname(File.expand_path(__FILE__)) 

class Foo
  def bar
    puts 'bar'
  end
end

foo = Foo.new
foo.bar
  • 普通にrubyで動作確認

    ruby foo.rb

    barと表示されます。

  • exe化

    ocra foo.rb

    foo.exeができます。

  • exe実行

    foo.exe

    barと表示されます。

64
64
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
64
64

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?