ruby c++
While looking for information on the subject, I looked into the ONLamp article Extending Ruby with C by Garrett Rooney, the Extending Ruby chapter in the Pickaxe, README.EXT (located at /usr/share/doc/ruby1.8-dev/README.EXT.gz on my Ubuntu system) and got some help from Kjetil.
The resulting file can be found here: wtmplist.c.
Disclaimer: I’m not resposible if you should destroy something with your newfound skills, nor am I responsible if you should suddenly get the urge to leave your wife/loved ones in favor of Ruby.
Table of Contents:
What to Extend?
How Do I Want the Interface to Be?
Get to it! #include <ruby.h>
Groundwork.
Making it Compile.
Collect the Data th Make Available.
Adding More Functionality.
Test Your New Library.
What to Extend?
Obviously you start by figuring out what you want to extend Ruby with. Some library missing, or better implemented in C? Some library you wrote yourself and want to be able to use from Ruby scripts?
I have a project coming up where I’ll have to look into /var/log/wtmp to get login entries, and didn’t find any classes/modules that made this possible in Ruby. There are documented (through Man) C functions to access the structures in the file, so I started to look at the introductions to extending Ruby (regrettably I somehow forgot about the Pickaxe at this point, but Kjetil reminded me after I had been cursing for a while).
Moving on to the next step …
How Do I Want the Interface to Be?
The point here is to think how you would like the information and methods represented in Ruby. In my case, I want to read in all entries in wtmp, and have them available in a list of some sort. In Ruby that means using the array. So an Array of entries doesn’t sound too bad. To make things cleaner, I’ll also put it in a module. This maps to this Ruby code (roughly):
module Wtmp
class List < Array
def initialize
# open wtmp
# wtmp.each_struct
Ïà¹ØÎĵµ£º
ÉÏһƪÎÄÕÂ
˵ÁËRubyµÄ°²×°ºÍÔËÐУ¬Ò²¼òµ¥µÄ˵ÁËÏÂÀàºÍ¶ÔÏó¡£ÕâÀïÖ÷Ҫ̸̸±äÁ¿µÄÎÊÌâ¡£
ÏÈ˵³£Á¿
¡£Èç¹û±äÁ¿ÃûÒÔ´óд×Öĸ¿ªÍ·£¬¾Í±»ÊÓΪ³£Á¿£¬µ«Í¨³£ÊÇËùÓÐ×Öĸ¶¼´óд¡£µ«ºÍÆäËûÓïÑÔ²»Í¬£¬ÔÚRubyÖУ¬ÄãÈÔÈ»¿ÉÒԸı䳣Á¿µÄÖµ£¬µ±È»½âÊÍÆ÷»áÅ׳öÒ»¸ö¾¯¸æ£º
#! /usr/bin/ruby
CONSTANT = 1
print "#{CONSTANT}\n&qu ......
Ruby 101£º·½·¨¶ÔÏó
Written by Allen Lee
´Ó·½·¨µ÷ÓÃ˵Æð
ÔÚÉÏһƪÎÄÕÂÀÎÒÃÇ¿´µ½µ÷ÓöÔÏóµÄ·½·¨ÊµÖÊÉÏÊÇÏò¶ÔÏó·¢ËÍÏûÏ¢£¬ÏÂÃ棬ÎÒÃÇÔÙÀ´¿´Ò»¸öÓÐȤµÄÓ¦Óá£ÔÚRubyÀ×Öµä¿ÉÒÔͨ¹ý {key => value} À´´´½¨£¬Èç¹ûÄãʹÓõİ汾ÊÇ1.9»òÒÔÉÏ£¬µ±keyµÄÀàÐÍÊÇSymbolʱ£¬´´½¨×ÖµäµÄÓ ......
Ö¸ÕëÊÇc/c++ÓïÑÔµÄÌØÉ«£¬¶øÊý×éÃûÓëÖ¸ÕëÓÐÌ«¶àµÄÏàËÆ£¬ÉõÖÁºÜ¶àʱºò£¬Êý×éÃû¿ÉÒÔ×÷ΪָÕëʹÓá£ÓÚÊǺõ£¬ºÜ¶à³ÌÐòÉè¼ÆÕ߾ͱ»¸ãºýÍ¿ÁË¡£¶øÐí¶àµÄ´óѧÀÏʦ£¬ËûÃÇÔÚcÓïÑԵĽÌѧ¹ý³ÌÖÐÒ²´íÎóµÃ¸øѧÉú½²½â£º\"Êý×éÃû¾ÍÊÇÖ¸Õë\"¡£ºÜÐÒÔË£¬ÎҵĴóѧÀÏʦ¾ÍÊÇÆäÖÐÖ®Ò»¡£Ê±ÖÁ½ñÈÕ£¬ÎÒÈÕ¸´Ò»ÈյؽøÐÐ×Åc/c++ÏîÄ¿µÄ¿ª·¢£¬¶øÉí± ......
JavaÓëC++×î´óµÄ²»Í¬µÄ£¬JavaÍêÈ«ÊÇÃæÏò¶ÔÏóµÄ£¬ÊÇÓÉÀ๹³ÉµÄ£¬¶øC++ÊÇÓɶÔÏóÓë¹ý³Ì×é³É¡£
1¡¢Â·¾¶£º
Java°üÀ¨Ò»¸ö°üpackage,C++ûÓУ»
2¡¢ÒýÈëÀà
java import.*.*; C++ include ""';
3¡¢Ô¤¶¨Ò壬
C++°üÀ¨Ô¤¶¨Ò壬Java²»°üÀ¨
4¡¢½á¹¹
C++°üÀ¨½á¹¹£¬JavaÖ»°üÀ¨À࣬½á¹¹Ìå¿ÉÒÔת»¯ÎªÀà¡£
5¡¢ÀàÃûÓëÎļþÃû
Jav ......
#include < ruby.h > //
static int id_sum;
int Values[] = {5, 10 ,15,-1,20,0};
static VALUE wrap_sum(VALUE args)
{
VALUE * values = (VALUE *) args;
VALUE summer = values[0];
VALUE max = values[1];
return rb_funcall(summer,id_sum,1,max);
}
static VALUE ......