http://rubyinline.rubyforge.org/RubyInline/
$ sudo gem install RubyInline [sudo] password for nishi: Successfully installed ZenTest-4.1.3 Successfully installed RubyInline-3.8.2 2 gems installed Installing ri documentation for ZenTest-4.1.3... Installing ri documentation for RubyInline-3.8.2... Installing RDoc documentation for ZenTest-4.1.3... Installing RDoc documentation for RubyInline-3.8.2...
http://blog.tkmr.org/tatsuya/show/299-ruby-c-rubyinline
に書かれているサンプルを実行するが、
inline_sample.rb:32: undefined method `plus_inline' for #<Test:0xb7bcf428> (NoMethodError)
になる。
気を取り直して gems に入っている example を実行したら動いた!
$ cp /usr/lib/ruby/gems/1.8/gems/RubyInline-3.8.2/example.rb .
$ ruby example.rb
# of iterations = 1000000, n = 5
user system total real
null_time 1.310000 0.920000 2.230000 ( 2.621937)
c 5.040000 2.790000 7.830000 ( 10.498556)
c-raw 5.240000 2.560000 7.800000 ( 8.104351)
c-alias 5.100000 2.700000 7.800000 ( 8.025086)
pure ruby 20.700000 12.360000 33.060000 ( 35.544498)
関数名によって動かなくなるらしい。
動かない場合:
#!/usr/bin/ruby require 'rubygems' require 'inline' class Test inline do |builder| builder.c " int plus_inline(int x, int y){ return x + y; } " end end p Test.new.plus_inline(1,1)
$ ruby inline_sample.rb inline_sample.rb:13: undefined method `plus_inline' for #<Test:0xb7b9f124> (NoMethodError)
動く場合
#!/usr/bin/ruby require 'rubygems' require 'inline' class Test inline do |builder| builder.c " int add(int x, int y){ return x + y; } " end end p Test.new.add(1,1)
$ ruby inline_sample.rb 2
-d オプション付きで実行するといろいろ警告が出ているが、これでいいらしい。
$ ruby -d inline_sample.rb Exception `LoadError' at /usr/local/lib/site_ruby/1.8/rubygems.rb:1113 - no such file to load -- rubygems/defaults/operating_system Exception `NoMethodError' at /usr/lib/ruby/1.8/rational.rb:78 - undefined method `gcd' for Rational(1, 2):Rational Exception `LoadError' at /usr/local/lib/site_ruby/1.8/rubygems/config_file.rb:34 - no such file to load -- Win32API Exception `LoadError' at /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31 - no such file to load -- inline RubyInline v 3.8.2 Exception `LoadError' at /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31 - no such file to load -- inline/Inline_Test_34ec.so Exception `LoadError' at /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:38 - no such file to load -- inline/Inline_Test_34ec.so /home/nishi/.ruby_inline/Inline_Test_34ec.so is up to date 2
ちゃんとherokuで使えるということがわかった。
$ ./script/generate scaffold item name:string $ echo "RubyInline" >> .gems
items_controller.rb
require 'inline' class Test inline do |builder| builder.c " int add(int x, int y) { return x + y; } " end end class ItemsController < ApplicationController # GET /items # GET /items.xml def index @items = Item.all @test = Test.new.add(1, 1) 以下略
items/index.html.erb
<h1>Listing items</h1> <p> test = <%= @test %> </p> 以下略
のようなものが git push heroku master; heroku db:migrate して http://app-name.heroku.com/items/ で動いた。