[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RubyToC Question

Phil Rhoades

6/5/2008 10:31:00 PM

People,

RubyToC installs OK:

# gem install RubyToC
Bulk updating Gem source index for: http://gems.rub...
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: phil@pricom.com.au

22 Answers

Phillip Gawlowski

6/5/2008 10:34:00 PM

0

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

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

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

Rob Biedenharn

6/5/2008 10:36:00 PM

0


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

> People,
>
> RubyToC installs OK:
>
> # gem install RubyToC
> Bulk updating Gem source index for: http://gems.rub...
> 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: phil@pricom.com.au


Try adding:

require 'rubygems'

before you require the ruby_to_ansi_c

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com


Phil Rhoades

6/5/2008 10:52:00 PM

0

Phillip and Rob,


Phillip Gawlowski wrote:
> -----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: phil@pricom.com.au

Phil Rhoades

6/5/2008 11:16:00 PM

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: phil@pricom.com.au

Rob Biedenharn

6/5/2008 11:58:00 PM

0

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 Rhoades wrote:

> 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: phil@pricom.com.au
>

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com


Ryan Davis

6/6/2008 6:55:00 PM

0


On Jun 5, 2008, at 16:57 , Rob Biedenharn 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.

Phil Rhoades

6/6/2008 7:19:00 PM

0

Ryan,


Ryan Davis wrote:
>
> On Jun 5, 2008, at 16:57 , Rob Biedenharn 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 Rhoades

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
E-mail: phil@pricom.com.au

Axel Etzold

6/6/2008 11:32:00 PM

0


-------- Original-Nachricht --------
> Datum: Sat, 7 Jun 2008 04:19:12 +0900
> Von: Philip Rhoades <phil@pricom.com.au>
> An: ruby-talk@ruby-lang.org
> Betreff: Re: RubyToC - Second Question

> Ryan,
>
>
> Ryan Davis wrote:
> >
> > On Jun 5, 2008, at 16:57 , Rob Biedenharn 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 Rhoades
>
> Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
> GPO Box 3411
> Sydney NSW 2001
> Australia
> E-mail: phil@pricom.com.au

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

or the rbplusplus gem

http://rubyforge.org/projects/r...

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

--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/mult...

Phil Rhoades

6/7/2008 1:14:00 PM

0

Axel,


Axel Etzold wrote:
> -------- Original-Nachricht --------
>> Datum: Sat, 7 Jun 2008 04:19:12 +0900 Von: Philip Rhoades
>> <phil@pricom.com.au> An: ruby-talk@ruby-lang.org Betreff: Re:
>> RubyToC - Second Question
>
>> Ryan,
>>
>>
>> Ryan Davis wrote:
>>> On Jun 5, 2008, at 16:57 , Rob Biedenharn 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 Rhoades
>>
>> Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275) GPO Box
>> 3411 Sydney NSW 2001 Australia E-mail: phil@pricom.com.au
>
> 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...
>
> or the rbplusplus gem
>
> http://rubyforge.org/projects/r...
>
> 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: phil@pricom.com.au

David Masover

6/7/2008 3:47:00 PM

0

On Friday 06 June 2008 14:19:12 Philip Rhoades 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.