Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

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


Ïà¹ØÎĵµ£º

C/C++Êý×éÃûÓëÖ¸ÕëÇø±ðÏê½â

Ö¸ÕëÊÇc/c++ÓïÑÔµÄÌØÉ«£¬¶øÊý×éÃûÓëÖ¸ÕëÓÐÌ«¶àµÄÏàËÆ£¬ÉõÖÁºÜ¶àʱºò£¬Êý×éÃû¿ÉÒÔ×÷ΪָÕëʹÓá£ÓÚÊǺõ£¬ºÜ¶à³ÌÐòÉè¼ÆÕ߾ͱ»¸ãºýÍ¿ÁË¡£¶øÐí¶àµÄ´óѧÀÏʦ£¬ËûÃÇÔÚcÓïÑԵĽÌѧ¹ý³ÌÖÐÒ²´íÎóµÃ¸øѧÉú½²½â£º\"Êý×éÃû¾ÍÊÇÖ¸Õë\"¡£ºÜÐÒÔË£¬ÎҵĴóѧÀÏʦ¾ÍÊÇÆäÖÐÖ®Ò»¡£Ê±ÖÁ½ñÈÕ£¬ÎÒÈÕ¸´Ò»ÈյؽøÐÐ×Åc/c++ÏîÄ¿µÄ¿ª·¢£¬¶øÉí± ......

C/C++ ÄÚÖÃÀàÐ͵ÄÊýÖµ·¶Î§

Data   Type   Ranges  
  C/C++   recognizes   the   types   shown   in   the   table   below.  
   
  Type   Name   Bytes   Other   Names   Range   of   Values    
&nb ......

×îÏêϸµÄRuby on Rails°²×°²½Öè


±¾ÎĽéÉÜÁËRuby on RailsµÄÏêϸ°²×°²½Öè¡£ÎÄÖÐʹÓõÄRuby on Rails°æ±¾ÊÇ1.8.6-26¡£°²×°RubyÖ®ºó°²×°Rails£¬È»ºó¾Í¿ÉÒÔ´´½¨WebÓ¦Ó㬲¢ÔÚ±¾µØ²âÊÔÁË¡£
1¡¢°²×°ruby
²»ÓÃ˵ ÊÇÏÂÔØ°²×°°ü£ºhttp://rubyforge.org/frs/?group_id=167£¬×¢Òâ°æ±¾---º¦È˲»Ç³£¬ºóÃæ»á˵µ½£¬ÎÒϵÄÊÇ1.8.6-26
×°Íêºó£¬¿ÉÒÔÓÃruby -v ²âÊÔÊÇ·ñ° ......

c++Öе÷ÓÃruby´úÂë

#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 ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ