RubyInline 1.0.6 has been released!
Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. It allows you to embed C external module code in your ruby
script directly. The code is compiled and run on the fly when needed.
The ruby version isn’t near as feature-full as the perl version, but it
is neat!
Changes:
- Added a prelude argument for header and datatype declarations.
- Only output stuff if invoked verbose (ie ‘ruby -v ./example.rb’).
- Added some compatibility code for windows.
- Declared requirement on POSIX system.
The ruby version isn’t near as feature-full as the perl version, but it
is neat!
Very neat !
- Added a prelude argument for header and datatype declarations.
Can you please show with an example how to use the “prelude” argument.
My (dumb) attempt was :
inline args, ‘#include <stdio.h>’,<<-END
…
END
But then what if I have multiple lines in the prelude?
- Added some compatibility code for windows.
I hate to bother you again …but I couldn’t make it work on Windows XP
(Home Edition).
I got this message:
C:\ruby\RubyInline-1.0.6>ruby -v example.rb
ruby 1.7.2 (2002-07-02) [i386-mswin32]
RubyInline 1.0.6
c:/ruby/lib/ruby/site_ruby/inline.rb:30:in inline': undefined method
+’
for nil (NoMethodError)
from example.rb:16:in fastfact' from example.rb:36 from example.rb:36:in
each’
from example.rb:36
Patch below for better error messages if the inline code fails to
compile:
Script started on Sun Sep 22 23:53:03 2002
batsman@kodos:/tmp/RubyInline-1.0.6$ diff -u inline.rb.old inline.rb
— inline.rb.old Sun Sep 22 23:50:46 2002
+++ inline.rb Sun Sep 22 23:50:41 2002
@@ -51,13 +51,15 @@
$stderr.puts “ERROR: Can’t find header files for ruby. Exiting…”
exit 1
end
···
On Thu, Sep 19, 2002 at 03:16:41PM +0900, Ryan Davis wrote:
RubyInline 1.0.6 has been released!
Ruby Inline is my quick attempt to create an analog to Perl’s
Inline::C. It allows you to embed C external module code in your ruby
script directly. The code is compiled and run on the fly when needed.
The ruby version isn’t near as feature-full as the perl version, but it
is neat!
RubyInline download | SourceForge.net
Changes:
- Added a prelude argument for header and datatype declarations.
- Only output stuff if invoked verbose (ie ‘ruby -v ./example.rb’).
- Added some compatibility code for windows.
- Declared requirement on POSIX system.
#include “ruby.h”
#{prelude}
static VALUE t_#{mymethod}(int argc, VALUE *argv, VALUE self) {
batsman@kodos:/tmp/RubyInline-1.0.6$ cat example.rb
#!/usr/local/bin/ruby -w
require “inline”
class MyTest
include Inline
def factorial(n)
f = 1
n.downto(1) { |x| f = f * x }
f
end
def fastfact(*args)
inline args, <<-END
aint i, f=1;
for (i = FIX2INT(argv[0]); i >= 1; i–) { f = f * i; }
return INT2FIX(f);
END
end
end
t = MyTest.new()
max = 1000000
puts “RubyInline #{Inline::VERSION}”
if ARGV.length == 0 then
type = “Inline”
tstart = Time.now
(1…max).each { |n| r = t.fastfact(5); if r != 120 then puts “ACK! - #{r}”; end }
tend = Time.now
else
type = “Native”
tstart = Time.now
(1…max).each { |n| r = t.factorial(5); if r != 120 then puts “ACK! - #{r}”; end }
tend = Time.now
end
total = tend - tstart
avg = total / max
printf “Type = #{type}, Iter = #{max}, time = %.8f sec, %.8f sec / iter\n”, total, avg
batsman@kodos:/tmp/RubyInline-1.0.6$ ruby example.rb
RubyInline 1.0.6
example.rb: In function t_fastfact': example.rb:17:
aint’ undeclared (first use in this function)
example.rb:17: (Each undeclared identifier is reported only once
example.rb:17: for each function it appears in.)
example.rb:17: parse error before i' example.rb:18:
i’ undeclared (first use in this function)
example.rb:18: `f’ undeclared (first use in this function)
Type = Inline, Iter = 1000000, time = 2.44084700 sec, 0.00000244 sec / iter
batsman@kodos:/tmp/RubyInline-1.0.6$
Script done on Sun Sep 22 23:53:48 2002
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
MSDOS didn’t get as bad as it is overnight – it took over ten years
of careful development.
– dmeggins@aix1.uottawa.ca
- Added a prelude argument for header and datatype declarations.
Can you please show with an example how to use the “prelude” argument.
My (dumb) attempt was :
inline args, ‘#include <stdio.h>’,<<-END
…
END
But then what if I have multiple lines in the prelude?
Either:
- embed \n in a double quoted (or other form of interpreted) string.
- just have a multi line string
- use the heredoc style string as above, but w/ a variable above the
inline call…
I got this message:
C:\ruby\RubyInline-1.0.6>ruby -v example.rb
ruby 1.7.2 (2002-07-02) [i386-mswin32]
RubyInline 1.0.6
c:/ruby/lib/ruby/site_ruby/inline.rb:30:in inline': undefined method
+’
for nil (NoMethodError)
from example.rb:16:in fastfact' from example.rb:36 from example.rb:36:in
each’
from example.rb:36
Define INLINEDIR or HOME in your environment. Better yet, get cygwin
and run in a POSIX environment. Windows does too many things on too
many platforms and I own NONE of them to test on.
···
On Thursday, September 19, 2002, at 08:40 PM, Shashank Date wrote:
“Ryan Davis” ryand@zenspider.com wrote in message
news:3B3EC5AD-CC71-11D6-8829-0003939161E2@zenspider.com…
Either:
- embed \n in a double quoted (or other form of interpreted) string.
Yes, that was a natural extension of my earlier attempt and I tried that.
- just have a multi line string
- use the heredoc style string as above, but w/ a variable above the
inline call…
That worked too ! Thanks…
I got this message:
C:\ruby\RubyInline-1.0.6>ruby -v example.rb
ruby 1.7.2 (2002-07-02) [i386-mswin32]
RubyInline 1.0.6
c:/ruby/lib/ruby/site_ruby/inline.rb:30:in inline': undefined method
+’
for nil (NoMethodError)
from example.rb:16:in fastfact' from example.rb:36 from example.rb:36:in
each’
from example.rb:36
Define INLINEDIR or HOME in your environment.
Got it …now it is working.
Better yet, get cygwin and run in a POSIX environment.
I can definitely do that at home … but I do not have that option at work.
Windows does too many things on too many platforms and I own NONE of them
to test on.
I agree … no problem.
Again, thanks for coming up this neat utility.
– shanko