23 Comments
- JaredRR, on 10/12/2007, -0/+10Compared to Java's JNI, Ruby's C extenstion mechanism is drop dead simple.
I had to do this recently and was amazed at how easy it was to join Ruby and C. In fact, I wrote my own blog entry on how easy it was. http://www.jaredrichardson.net/blog/2006/03/25/#C-extenstions - nicholaides, on 10/12/2007, -2/+8Yet another reason to use Ruby.
- craigly, on 10/12/2007, -0/+5I don't know if zenspider has an account and I don't want to steal his thunder, but check out how much easier he makes it for Rubyists to create a C extension using Inline. Dead simple: http://blog.zenspider.com/archives/2006/08/writing_c_exten.html
- catoutfit, on 10/12/2007, -4/+8@PJBonoVox
A comment, where it's only purpose is to describe it's self...wow.. - ev0lhanson, on 10/12/2007, -0/+4You can do inline assembly in C.
http://www-128.ibm.com/developerworks/library/l-ia.html
You can do inline C in Ruby as well.
http://www.zenspider.com/ZSS/Products/RubyInline/
So you could have inline C in ruby that contains inline assembly. That's pretty cool. - esconsult1, on 10/12/2007, -5/+8Quick. Clean and useful.
- sbrown123, on 10/12/2007, -0/+3There are other options in Java rather than writing JNI to call C functions. For example ctypes4j (http://ctypes4j.sourceforge.net/) or SWIG. The .NET implementation, from what I have seen, is probably the best though (if portability is not a worry).
- springfield, on 10/12/2007, -0/+2Whilst we are on the subject of C/Ruby: http://vela.sf.net has some great examples of how to embed the Ruby interpreter into your C application.
- johneffort, on 06/06/2008, -0/+1I didn't know this was a war?
Anyway, In ruby you can just do the same as someone commented in the original article:
require 'rubygems'
require 'inline'
inline(:C) do |builder|
builder.c "int method_test1() {
int x = 10;
return x;
}"
end - KCorax, on 10/12/2007, -1/+2Riiight, the road is open now to turn Ruby into ISAPI ?
I really don't get why one wouldn't want to write an .so or com component and interface with it directly. This way you would get compatibility with most major programming languages.
PJBonoVox is right... - stoffe, on 10/12/2007, -0/+1Well, I tried some simple examples and it just worked(TM). Very, very cool. Especially since it's possible to package up the finished extension as a separate binary library. :)
Hopefully some more functionality will be added for easy adding of extra source files (making bindings etc), it's already possible from looking at the source, but not very convenient...
Also, the guy behind this seems to have a ruby->C->compile optimizer in the works too... wow. - Matadon, on 10/12/2007, -0/+1I knew there was a reason I picked up Ruby! I remember trying to write a new module in C for Perl...utter hell compared to this.
All I need now is a way to produce a binary from Ruby code for the Holy Trinity (Windows, MacOS, and Linux), and I can just stop developing in any other language. *grin* - CaughtThinking, on 10/12/2007, -0/+1Basically people don't code in C anymore because it's too damn easy to be too crappy at it. It's last frontier are intense desktop applications like games, or processors of huge datasets. Even for those, Java can fit it well. For the applications where this is absolutely necessary, the Java community has already created stable extensions based on C. (for instance the Lightweight Java Gaming Library)
Ruby is yet another scripting language for you to use so you can avoid the C/C++ level, and code "easier". Yet, here we have Ruby hooks into C, so you can get back into the nightmare everyone was trying to avoid...
There is no reason to Digg this basically. A C hook is a "last resort" for modern programming, but the Diggers are so obsessed with Ruby that they take it as gold.
Got it? :) - VyRuZ, on 10/12/2007, -1/+1Many coders will reach a situation where developing a C extension makes sense, whether for doing 'heavy lifting', diving into assembly language...
Excuse me, but since when does C code count as assembly ? - CaughtThinking, on 10/12/2007, -1/+1In Java you dont have to go to C for performance. Ruby's only possibility for high performance is to go to C. I guess this is one more supposed benefit for the Rails community to get zealous about and over-glamorize.
Ruby is Perl allll over again. - jinushaun, on 10/12/2007, -2/+2Unfortunately, their example C extension is stupidly easy. A custom C extension that has one method that returns the number 10? Please.
Dugg down.
At least the C code looks 'prettier' than a Python extension written in C. - clip9, on 10/12/2007, -2/+1Ha.. Perl wins again.
use Inline C =>'END_OF_C';
long return_10 () {
retrun 10;
}
END_OF_C; - kynky, on 10/12/2007, -1/+0AFAIK, Ruby is RAD, Compiling can be seen as a weak form of TDD, Unit testing is a must. Ruby is amazing for OOP, for large scalable projects. Can't wait for Rite, to make Ruby faster. Inline Ruby is amazing, compiling the C in the Ruby Code on fly with same version of gcc that compiled the VM (I think). IMHO if you need todo something processor intensive like a mathematical algorithm, then C is the way, but then integrate it into the larger environment using Ruby as the glue.
- SamKellett, on 10/12/2007, -5/+3Shout names as much as you want PJBonoVox, but you sir, are blocked.
- kurrent, on 10/12/2007, -6/+3I wish i knew more about programming and had a single clue about what these kind of diggs are about.
I'm sure they're interesting and would make good reads. - catoutfit, on 10/12/2007, -5/+1PJBonoVox...*blocked*
- inactive, on 10/12/2007, -11/+2@catoutfit
You're a ***** retard. 'What you say is what you are...'.
Dick. - inactive, on 10/12/2007, -28/+6Pointless. Boring and buried.


What is Digg?