开发者

Is there a way to translate C code to Ruby?

I'm sitting with massive amounts of legacy C code that needs to be converted to Ruby for a project.

I've seen Ruby to C translators online, but not the other way around. Would there be a sim开发者_运维问答ple way to approach this particular problem?


You'll either have to write a C to Ruby translator, which is possible but the effort might not be justifiable, or you could split the C code up into smaller modules that you can create Ruby wrappers for as a first step. Once they're all wrapped in Ruby and the main control flow is done in Ruby, you can write a test harness (both to verify correctness of your replacement code and to aid reverse engineering) and start replacing the C modules with Ruby modules.

The divide & conquer approach should work with regular Ruby if you use the modules as native extensions but obviously this will cause further problems if you're targeting something like JRuby as your runtime environment. If you want to do something similar in JRuby as per your comment, you're looking at wrapping the C modules in JNI and calling through from the JVM that way. Either way will allow your C code to interact with the Ruby code, but the two approaches are not interchangeable.

Neither approach is going to be quick and both are going to be a lot of work.


You could:

  1. Write a C interpreter in Ruby (very hard).
  2. Wrap the compiled C code with something like SWIG (much easier).


Programming in C and programming in Ruby bear completely different programming paradigms. So while the old saying that you can write Fortran (or C in this case) code in any language is true, the Ruby code that you would eventually get by machine translation wouldn't be Ruby at all, except syntactically.

So, IMHO, any way other than manual (and done by proficient Rubyists, I might add) would be either impossible, or at least not useful at all.


Maybe you can try this https://github.com/jackieju/CPP2Ruby/ It's fresh for cpp but existed longer for c to ruby


You can try Abap2Ruby. It can also convert cpp to ruby.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜