require "rubygems"
require "inline"
class Primes
inline do |builder|
builder.c '
int prime(int num) {
if (num == 2){ return 1;}
if (num %2 == 0) {return 0;}
int i = 3;
while( i * i <= num)
{
if (num % i==0 )
{
return 0;
}
i = i+2;
}
return 1;
}'
end
end
p = Primes.new
for num in 2..10_000 do
is_prime = p.prime(num)
if is_prime == 1
puts "#{num} is a prime number"
else
puts "#{num} equals #{is_prime} * #{num/is_prime}"
end
end
在执行这段ruby代码时,我得到了很多错误…
/.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:96: warning: else without rescue is useless
.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:108: warning: else without rescue is useless
.rvm/rubies/ruby-1.9.3-p362/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require':/.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:93: syntax error, unexpected ':', expecting keyword_then or ',' or ';' or '\n' (SyntaxError)
when nil: @position = length-1
^
.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:94: syntax error, unexpected keyword_when, expecting keyword_end
when 0: @position = length-1 if @cycle
^
.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:105: syntax error, unexpected ':', expecting keyword_then or ',' or ';' or '\n'
when nil: @position = length-1
^
.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:106: syntax error, unexpected keyword_when, expecting keyword_end
when length-1: @position = 0 if @cycle
^
.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:106: syntax error, unexpected ':', expecting keyword_end
when length-1: @position = 0 if @cycle
^
.rvm/gems/ruby-1.9.3-p362/gems/inline-0.1.0/lib/inline/history_buffer.rb:128: syntax error, unexpected keyword_end, expecting $end
end
^
最佳答案
你用的是废弃宝石的旧版本。Seattlerb团队从未创建过名为RubyInline
的版本。看看这个。