RubyToC Question

P

Philip Rhoades

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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
P

Phillip Gawlowski

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

Philip Rhoades 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 Gawlowski
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-----
 
R

Rob Biedenharn

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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)


Try adding:

require 'rubygems'

before you require the ruby_to_ansi_c

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
P

Philip Rhoades

Phillip and Rob,


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

Philip Rhoades 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?


Muchas gracias!

Phil.

--
Philip Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
P

Philip Rhoades

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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
R

Rob Biedenharn

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;
}

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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Ryan Davis

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.
 
P

Philip Rhoades

Ryan,


Ryan said:
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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Sat, 7 Jun 2008 04:19:12 +0900
Von: Philip Rhoades <[email protected]>
An: (e-mail address removed)
Betreff: Re: RubyToC - Second Question
Ryan,





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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)

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
 
P

Philip Rhoades

Axel,


Axel said:
-------- Original-Nachricht --------


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 ?


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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 
D

David Masover

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.
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Sat, 7 Jun 2008 22:14:13 +0900
Von: Philip Rhoades <[email protected]>
An: (e-mail address removed)
Betreff: Re: RubyToC - Second Question
Axel,




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 :)

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
 
P

Philip Rhoades

Axel,


Axel said:
-------- Original-Nachricht --------


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


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.

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 . .

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.

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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top