<noscript><div
class="greet_block"><div class="greet_text"><div class="greet_image"><a
href="http://www.rubyinside.com/feed/rss" rel="nofollow"><img
src="http://www.rubyinside.com/wp-content/plugins/wp-greet-box/images/rss_icon.png"
alt="WP Greet Box icon"/></a></div>Hello there! If you are new here,
you might want to <a href="http://www.rubyinside.com/feed/rss"
rel="nofollow"><strong>subscribe to the RSS feed</strong></a> for
updates on this topic.<div style="clear:both"></div><div
class="greet_block_powered_by"><a
href="http://omninoggin.com/projects/wordpress-plugins/wp-greet-box-wordpress-plugin/"
title="WP Greet Box WordPress Plugin">Powered by WP Greet
Box</a></div><div style="clear:both"></div></div></div></noscript>
Many
coders will reach a situation where developing a C extension makes
sense, whether for doing 'heavy lifting', diving into assembly
language, interfacing with other C code, etc. Luckily, developing a
basic Ruby extension in C is easy.
Note: This article assumes you are using a UNIX of some sort
(this was all tested on OS X) and that you have Ruby installed properly
(from source, ideally, so you have
ruby.h
available). If not, you may be stuck.
First, create a directory called MyTest (or whatever you want your extension to be called) and in there create two files, extconf.rb
and MyTest.c
(if you want to download pre-written sources, they're in this tar file
). In extconf.rb, put the following:
require
'
mkmf
'
extension_name
=
'
mytest
'
dir_config
(
extension_name
)
create_makefile
(
extension_name
)
This code is pretty self descriptive. It loads up Ruby's makefile
library, sets up the environment, and creates a Makefile. Next, create
the actual extension in MyTest.c
. Here's some demonstration code to create a basic module with a single method called 'test1' which returns '10' when called:
// Include the Ruby headers and goodies
#include "ruby.h"
// Defining a space for information and references about the module to be stored internally
VALUE MyTest = Qnil;
// Prototype for the initialization method - Ruby calls this, not you
void Init_mytest();
// Prototype for our method 'test1' - methods are prefixed by 'method_' here
VALUE method_test1(VALUE self);
// The initialization method for this module
void Init_mytest() {
MyTest = rb_define_module("MyTest");
rb_define_method(MyTest, "test1", method_test1, 0);
}
// Our 'test1' method.. it simply returns a value of '10' for now.
VALUE method_test1(VALUE self) {
int x = 10;
return INT2NUM(x);
}
For C, it's reasonably simple code. We include the Ruby headers via ruby.h
,
set up a variable to store the module in, and create two functions, one
which is called by Ruby when it initiates the module, and the other is
our test1
method (Note that in the code above we define
MyTest as a module, but you could just as easily use rb_define_class to
create a class, if that's what you wanted to do.)
Next we need to compile our hard work. Make sure you're in the MyTest directory and run ruby extconf.rb
, and it should say that the Makefile has been created. If so, you can then run make
to compile and build the extension. As long as no errors occur, run up
irb (or create a new Ruby program) and test out your newly build
extension like so:
irb(main):001:0> require 'mytest'
=> true
irb(main):002:0> include MyTest
=> Object
irb(main):003:0> puts test1
10
Et voila! The world's simplest Ruby extension written in C in under
5 minutes. It doesn't do much, it's overly basic, but this is the
springboard to greater things :)
相关推荐
Simple program demonstrating how to create a stop watch using the Timer control.
How to Create a Database in Python using SQL Lite 3 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
How to create a ECB Menu in SharePoint 2013 Manully
How to create a directory tree like in Explorer
Smashing eBook #5: How to Create Selling E-Commerce Websites 3 Preface Designing and developing for e-commerce takes a lot of brain work, knowledge and research before you start. You don‘t only have...
How to create a directory tree like in Explorer(19KB)
How to create a donut chart like this in chart.js
How to Create an Object Class in MATLAB.zip
How to create a database in python using sql lite 3.pdf
How to Create a Database in Python using SQL Lite 3 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
How to create a custom preview form
How to Create a Database in Python using SQL Lite 3 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
How to Create a Database in Python using SQL Lite 3 英文无水印转化版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或...
How to create a shortcut to another file
This shows how to create a 3D Maze type of game in VB.
How to use epoll A complete example in C How to use epoll A complete example in C How to use epoll A complete example in C How to use epoll A complete example in C
An example of how to create a coolbar such as the one in Internet Explorer without using a 3rd party control.
This demonstrates how to create a short-cut to a specific file.
At first glance, learning how to use Ruby can seem fairly simple. Developers around the world find Ruby’s syntax to be graceful and straightforward. You can express algorithms in a very natural way, ...
How to Create a Healthy Plate - English.srt