Using Ruby with other languages

Hi all
I’m new to Ruby, but before seriously studying the language I would like
to get some information.

I would like to know, whether it is possible to compile Ruby code to
executables so I could run Ruby code without the interpreter. I also
would like to do this, when I noticed that a simple Ruby program runs
more than 100 times slower than identically implemented C program. I
also would like to know, wheter it is possible to compile RUby code to
object format and then link with code written in different languages,
EG. C++ and C.

Thanks for all answers!

···


Tapio

Tapio Kelloniemi wrote:

also would like to know, wheter it is possible to compile RUby code to
object format and then link with code written in different languages,
EG. C++ and C.

You can link ther ruby interpreter with your (C,C++) program,
source ruby code into your program.

You can also link your (C,C++) library as a DLL and use it from
Ruby.

Regards, Christian

Hi,

I would like to know, whether it is possible to compile Ruby code to
executables so I could run Ruby code without the interpreter.

If you mean to a stand-alone executable, not currently. Python generates
some object code (byte code?), but I think it still needs the Python
interpreter. To me this is an interesting question, whether even some
small, limited Ruby to C translator can be written. Based on my initial
experience writing Ruby in C, my feeling says yes, but probably it is
wrong.

I also
would like to do this, when I noticed that a simple Ruby program runs
more than 100 times slower than identically implemented C program.

Currently there is some initial release of Ruby “inline” that can be used
to insert C code in the middle of Ruby code to speed up the critical
parts. Creating a Ruby/C code that is as fast as a (comparable
algorithmically) C code is impossible, I think, because Ruby is
garbage-collected language and there is some overhead required by the
Ruby object model.

I
also would like to know, wheter it is possible to compile RUby code to
object format and then link with code written in different languages,
EG. C++ and C.

As Ruby itself is written in C, linking to other code or library in C or
C++ has been done all the time; however, currently you need to learn some
Ruby C API to do that, or browse the SWIG web site (www.swig.org).

Regards,

Bill

···

Tapio Kelloniemi spam07@thack.org wrote:

Seems like there should be something in the FAQ about this.

Gavin

···

----- Original Message -----
From: “Tapio Kelloniemi” spam07@thack.org
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, September 24, 2002 4:38 AM
Subject: Using Ruby with other languages

Hi all
I’m new to Ruby, but before seriously studying the language I would like
to get some information.

I would like to know, whether it is possible to compile Ruby code to
executables so I could run Ruby code without the interpreter. I also
would like to do this, when I noticed that a simple Ruby program runs
more than 100 times slower than identically implemented C program. I
also would like to know, wheter it is possible to compile RUby code to
object format and then link with code written in different languages,
EG. C++ and C.

Thanks for all answers!


Tapio

Hello Tapio,

Monday, September 23, 2002, 10:38:37 PM, you wrote:

I’m new to Ruby, but before seriously studying the language I would like
to get some information.

I would like to know, whether it is possible to compile Ruby code to
executables so I could run Ruby code without the interpreter. I also
would like to do this, when I noticed that a simple Ruby program runs
more than 100 times slower than identically implemented C program. I
also would like to know, wheter it is possible to compile RUby code to
object format and then link with code written in different languages,
EG. C++ and C.

  1. are you know difference between compilation and interpretation? are
    you know assembler? are you ever seen internals of any interpreter?

on simplest code i see 1000 times difference between C and Ruby. i.e.
to execute one simple Ruby instruction, interpreter had to execute
1000 lines of C code. this leads to 1 million of simple Ruby
instructions per second - enough performance for 90% of code for
commercial program and more than enough for almost any in-house
scripting

this is argument for me to switch to interpreted languages
(considering that ruby reduces development time). but if you want to
be absolutely fast, see at c++ or eiffel

  1. there is many solutions to combine c and ruby (and other languages)
    code:

a) you can write libraries for Ruby in C and link its dynamically or
statically in interpreter itself. see readme.ext in distribution

b) you can inline C code in ruby programs (see RubyInline on yaraa)

c) you can translate Ruby code to C (see rb2c). this don’t speed up
your program, but helps to make single executable

d) you can use exerb, which adds Ruby code inside single executable in
another way

···


Best regards,
Bulat mailto:bulatz@integ.ru

Also look at exerb. http://exerb.sourceforge.jp/index.en.html
It’s not perfect, but it does pretty well overall.

  • alan
···

On Tue, Sep 24, 2002 at 04:39:03AM +0900, Christian Szegedy wrote:

Tapio Kelloniemi wrote:

also would like to know, wheter it is possible to compile RUby code to
object format and then link with code written in different languages,
EG. C++ and C.

You can link ther ruby interpreter with your (C,C++) program,
source ruby code into your program.

You can also link your (C,C++) library as a DLL and use it from
Ruby.

Regards, Christian


Alan Chen
Digikata LLC
http://digikata.com

Hello William,

Tuesday, September 24, 2002, 12:19:11 AM, you wrote:

Hi,

I would like to know, whether it is possible to compile Ruby code to
executables so I could run Ruby code without the interpreter.

If you mean to a stand-alone executable, not currently. Python generates
some object code (byte code?), but I think it still needs the Python
interpreter. To me this is an interesting question, whether even some
small, limited Ruby to C translator can be written. Based on my initial
experience writing Ruby in C, my feeling says yes, but probably it is
wrong.

see rb2c on yaraa, it does exactly what you want

···

Tapio Kelloniemi spam07@thack.org wrote:


Best regards,
Bulat mailto:bulatz@integ.ru

on simplest code i see 1000 times difference between C and Ruby. i.e.
to execute one simple Ruby instruction, interpreter had to execute
1000 lines of C code. this leads to 1 million of simple Ruby
instructions per second - enough performance for 90% of code for
commercial program and more than enough for almost any in-house
scripting

You are correct: simple programs are the slowest (realtively),
but if you use higher level functions (built in functionality),
then the slowdown is much less (about 10-50x) than e.g.
in a number crunching algorithm implemented purely in Ruby.

Regards, Christian

Hello Christian,

Tuesday, September 24, 2002, 12:40:20 PM, you wrote:

on simplest code i see 1000 times difference between C and Ruby. i.e.
to execute one simple Ruby instruction, interpreter had to execute
1000 lines of C code.

You are correct: simple programs are the slowest (realtively),
but if you use higher level functions (built in functionality),
then the slowdown is much less (about 10-50x) than e.g.
in a number crunching algorithm implemented purely in Ruby.

hmmm. i try to say that Ruby overload is about 1000 asm instructions
per method call, and someone had to see is this appropriate for
particular program or part of program

i personally started from thought that interpreted languages can be
used for 90% of code in large projects, switched for this from perl to
ruby and still feel that i need more strict language while performance
is more subject of algorithms, data structures and system libraries

···


Best regards,
Bulat mailto:bulatz@integ.ru