RubyToC Question

People,

RubyToC installs OK:

gem install RubyToC

Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed RubyToC-1.0.0.5
Installing ri documentation for RubyToC-1.0.0.5…
Installing RDoc documentation for RubyToC-1.0.0.5…

However when I try and run an example script:

require ‘ruby_to_ansi_c’
class MyTest
def say_hello
puts “hello”
end

def main
say_hello
return 0
end
end

I get:

$ ./truby2c.rb
./truby2c.rb:3:in `require’: no such file to load – ruby_to_ansi_c
(LoadError)
from ./truby2c.rb:3

What is wrong?

Thanks,

Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Philip R. wrote:

| $ ./truby2c.rb
| ./truby2c.rb:3:in `require’: no such file to load – ruby_to_ansi_c
| (LoadError)
| from ./truby2c.rb:3
|
| What is wrong?

Is “require ‘rubygems’” necessary for you?


Phillip G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ Know what I pray for? The strength to change what I can, the
inability to accept what I can’t and the incapacity to tell the
difference. – Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkhIahkACgkQbtAgaoJTgL+wFQCff6aL8QfaOvnb5NgRFxtCmbbO
cm8An0s+5VHDNXSOy1E+Nq6eYOwhlGpX
=I2fc
-----END PGP SIGNATURE-----

On Jun 5, 2008, at 6:30 PM, Philip R. wrote:

However when I try and run an example script:
end

E-mail: [email protected]
Try adding:

require ‘rubygems’

before you require the ruby_to_ansi_c

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Phillip and Rob,

Phillip G. wrote:

| What is wrong?

Is “require ‘rubygems’” necessary for you?

Muchas gracias!

Phil.


Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

It looks like the preamble is missing. The one in the rubyforge doc
doesn’t quite cut it since make doesn’t find ruby.h (I’m sure a CFLAGS
change would fix that).

Here’s a minimal set of changes that makes for a clean compile:

  • Add the first 3 lines
  • change the typecast on malloc to be str*
  • change long to int for main (prototype and definition)

Perhaps you want to check out treetop or one of the other parsers as
this project looks like it hasn’t been touched in 2 years.

-Rob

#include <stdio.h>
#include <stdlib.h>
typedef char * str;

void arr_iterate();
int main();

void
arr_iterate() {
str * tstarr;
tstarr = (str *) malloc(sizeof(str) * 3);
tstarr[0] = “this is the first line”;
tstarr[1] = “this is the second line”;
tstarr[2] = “last line”;
unsigned long index_line;
for (index_line = 0; tstarr[index_line] != NULL; ++index_line) {
str line = tstarr[index_line];
puts(line);
}
}

int
main() {
arr_iterate();
return 0;
}

On Jun 5, 2008, at 7:15 PM, Philip R. wrote:

class MyTest
arr_iterate
void arr_iterate();
for (index_line = 0; tstarr[index_line] != NULL; ++index_line) {

truby2c.c:17: error: ‘line’ undeclared (first use in this function)
Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

Rob B. http://agileconsultingllc.com
[email protected]

People,

truby2c.rb:

#!/usr/bin/ruby

require ‘rubygems’
require ‘ruby_to_ansi_c’

class MyTest
def arr_iterate
tstarr = [ ‘this is the first line’, ‘this is the second line’,
‘last line’ ]

 tstarr.each { |line|
     puts line
 }

end

def main
arr_iterate
return 0
end
end

result = RubyToAnsiC.translate_all_of MyTest
puts result

gives C output:

void arr_iterate();
long main();

void
arr_iterate() {
str * tstarr;
tstarr = (str) malloc(sizeof(str) * 3);
tstarr[0] = “this is the first line”;
tstarr[1] = “this is the second line”;
tstarr[2] = “last line”;
unsigned long index_line;
for (index_line = 0; tstarr[index_line] != NULL; ++index_line) {
str line = tstarr[index_line];
puts(line);
}
}

long
main() {
arr_iterate();
return 0;
}

compiling with gcc gives:

truby2c.c: In function ‘arr_iterate’:
truby2c.c:9: error: ‘str’ undeclared (first use in this function)
truby2c.c:9: error: (Each undeclared identifier is reported only once
truby2c.c:9: error: for each function it appears in.)
truby2c.c:9: error: ‘tstarr’ undeclared (first use in this function)
truby2c.c:10: error: expected ‘;’ before ‘malloc’
truby2c.c:16: error: expected ‘;’ before ‘line’
truby2c.c:17: error: ‘line’ undeclared (first use in this function)
truby2c.c: In function ‘main’:
truby2c.c:22: warning: return type of ‘main’ is not ‘int’

  • seems like some fundamental problems with such a little program?

Thanks,

Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

Ryan,

Ryan D. wrote:

On Jun 5, 2008, at 16:57 , Rob B. wrote:

Perhaps you want to check out treetop or one of the other parsers as
this project looks like it hasn’t been touched in 2 years.

it’s been touched, but none of the changes have been released.

ruby2c has a lot of dark corners and isn’t meant for general use. expect
a lot to break.

The reason I am interested is that I have CPU and IO intensive C/C++
genetics simulation program that I would love to rewrite in Ruby but it
probably would not be viable from a speed point of view. I thought
maybe if I could develop/debug etc to a working Ruby app and then, when
it is a going concern, turn it into a faster, compiled binary, I would
get the best of both worlds . .

Regards,

Phil.


Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

On Jun 5, 2008, at 16:57 , Rob B. wrote:

Perhaps you want to check out treetop or one of the other parsers as
this project looks like it hasn’t been touched in 2 years.

it’s been touched, but none of the changes have been released.

ruby2c has a lot of dark corners and isn’t meant for general use.
expect a lot to break.

Axel,

Axel E. wrote:

Ruby but it probably would not be viable from a speed point of
Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275) GPO Box

http://rubyforge.org/projects/rbplusplus/

There have been rumours on this list some time ago that SWIG could
also “wrap” Ruby into C, but I couldn’t confirm this from the docs.

I think the rbplusplus gem is very comfortable, but it’s still work
in progress … 0.1.1 doesn’t seem to support struct for instance,
but the last build from git does…

What is it that’s most time-consuming in your application ?

I am thinking about a complete rewrite (to better simulate the real
world) but I would be quite unenthusiastic about doing it in C++ and
also it would not help very much to be able to re-use parts of the
existing stuff via swig or rb++.

What does ‘“wrap” Ruby into C’ mean?

A rewritten application would be quite different but the existing app
has an array whose cells point to lists of parental and offspring
objects that completely change with each generation. Output is
continuously written to disk.

I would love to produce a new version in Ruby because writing OO in Ruby
is so much nicer but it would be just too slow and I am not really
interested in writing parts of it in C/C++ . . defeating the purpose of
rewriting really . .

Thanks,

Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

-------- Original-Nachricht --------

Datum: Sat, 7 Jun 2008 04:19:12 +0900
Von: Philip R. [email protected]
An: [email protected]
Betreff: Re: RubyToC - Second Question

it’s been touched, but none of the changes have been released.
get the best of both worlds . .
Sydney NSW 2001
Australia
E-mail: [email protected]

Philip,

if you have a lot of C/C++ code and look for wrappers, you could also
try SWIG

http://www.swig.org/Doc1.3/Ruby.html

or the rbplusplus gem

http://rubyforge.org/projects/rbplusplus/

There have been rumours on this list some time ago that SWIG could also
“wrap” Ruby into C, but I couldn’t confirm this from the docs.

I think the rbplusplus gem is very comfortable, but it’s still work in
progress … 0.1.1 doesn’t seem
to support struct for instance, but the last build from git does…

What is it that’s most time-consuming in your application ?

Best regards,

Axel

-------- Original-Nachricht --------

Datum: Sat, 7 Jun 2008 22:14:13 +0900
Von: Philip R. [email protected]
An: [email protected]
Betreff: Re: RubyToC - Second Question

expect a lot to break.
Phil.

in progress … 0.1.1 doesn’t seem to support struct for instance,
but the last build from git does…

What is it that’s most time-consuming in your application ?

Philip,

I am thinking about a complete rewrite (to better simulate the real
world) but I would be quite unenthusiastic about doing it in C++ and
also it would not help very much to be able to re-use parts of the
existing stuff via swig or rb++.

That sounds like you want to do some Ruby coding, just because it’s
so elegant. Very nice :slight_smile:

But as Dave said in his post, as Ruby doesn’t have a reputation as a
speed demon, you can throw out some computation-heavy things
to C/C++.
I was asking about what it is that takes the most time in your program,
because I suspect that you might be able to get to quick results by
reformulating the problem so that you can use Ruby bindings for some
numerical
software … like GSL or constraint programming (gecode)… (which are
implemented
in C/C++/Fortran) and then, you might not even have to bother about how
to deal with C
and still have quick execution.

What does ‘“wrap” Ruby into C’ mean?

Well, sort of writing Ruby and generate C from it using SWIG. What
Ruby2c promises
to do. It doesn’t seem to be possible.

A rewritten application would be quite different but the existing app
has an array whose cells point to lists of parental and offspring
objects that completely change with each generation. Output is
continuously written to disk.

There are also genetic algorithm projects for Ruby on Rubyforge.
I haven’t tried any of them yet.

I would love to produce a new version in Ruby because writing OO in Ruby
is so much nicer but it would be just too slow and I am not really
interested in writing parts of it in C/C++ . . defeating the purpose of
rewriting really . .

Well, if Ruby is slower in execution but quicker in writing, maybe one
can
spend some time profiling how to prune the search space …

Best regards,

Axel

On Friday 06 June 2008 14:19:12 Philip R. wrote:

The reason I am interested is that I have CPU and IO intensive C/C++
genetics simulation program that I would love to rewrite in Ruby but it
probably would not be viable from a speed point of view.

First, you could try Ruby 1.9. Probably more reliable than ruby2c, and
much
faster than Ruby 1.8.

But probably the simplest Ruby speed hack is to rewrite the more
performance-intensive parts as C extensions, which you call from Ruby.
That’s
common for Python and Perl, too. But premature optimization is evil –
write
it in Ruby first, then profile it to find out what actually needs to be
C.

Another way would be to keep some sort of base engine in C, and embed
Ruby
into it. This is common for games – write the game engine in C/C++,
with
some inline assembly, and add a scripting language (often Lua or Python)
to
do the actual game logic.

Axel,

Axel E. wrote:

Datum: Sat, 7 Jun 2008 04:19:12 +0900 Von: Philip R.

it’s been touched, but none of the changes have been
worlds . .

could also “wrap” Ruby into C, but I couldn’t confirm this from

I am thinking about a complete rewrite (to better simulate the real
world) but I would be quite unenthusiastic about doing it in C++
and also it would not help very much to be able to re-use parts of
the existing stuff via swig or rb++.

That sounds like you want to do some Ruby coding, just because it’s
so elegant. Very nice :slight_smile:

Yes, as always with apps, the hindsight and other developments suggest
improvements for a second version . .

But as Dave said in his post, as Ruby doesn’t have a reputation as a
speed demon, you can throw out some computation-heavy things to
C/C++.

In my case, it is hard to see how much would be left as Ruby . .

I was asking about what it is that takes the most time in your
program, because I suspect that you might be able to get to quick
results by reformulating the problem so that you can use Ruby
bindings for some numerical software … like GSL or constraint
programming (gecode)… (which are implemented in C/C++/Fortran) and
then, you might not even have to bother about how to deal with C and
still have quick execution.

Those things are interesting but unfortunately my stuff is sufficiently
obscure and specific that I have to do the low level stuff - otherwise I
would have used one of the existing simulation packages.

What does ‘“wrap” Ruby into C’ mean?

Well, sort of writing Ruby and generate C from it using SWIG. What
Ruby2c promises to do. It doesn’t seem to be possible.

Bit sad, would have been nice . .

A rewritten application would be quite different but the existing
app has an array whose cells point to lists of parental and
offspring objects that completely change with each generation.
Output is continuously written to disk.

There are also genetic algorithm projects for Ruby on Rubyforge. I
haven’t tried any of them yet.

Again, not something that is useful in my situation.

I would love to produce a new version in Ruby because writing OO in
Ruby is so much nicer but it would be just too slow and I am not
really interested in writing parts of it in C/C++ . . defeating the
purpose of rewriting really . .

Well, if Ruby is slower in execution but quicker in writing, maybe
one can spend some time profiling how to prune the search space …

I think I might try and do some prototyping with v1.9 and see how it
goes but I am actually supposed to be crunching and writing up results
with the existing stuff . .

Many thanks for the input though!

Regards,

Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: [email protected]

Philip R.:

The reason I am interested is that I have CPU and IO intensive C/C++
genetics simulation program that I would love to rewrite in Ruby but
it probably would not be viable from a speed point of view.

As David M. already pointed out, an approach you could consider is
to write everything in Ruby first, then profile using ruby-prof, see
which stuff calls which other stuff way too much and optimise this, then
speed up the outstanding slow parts by adjusting their algorithms, and
only then, if you can’t speed them up any more algorythmically, use
RubyInline to rewrite the bottlenecks (in place!) to C.

This is the approach I took when writing my PhD – but I can’t say I got
to the ‘using RubyInline’ part yet, so it can’t be taken as a working
example…

– Shot