Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Extending Ruby with C
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Paul Mucur
May 08, 2012
Programming
290
1
Share
Extending Ruby with C
A Learning Lunch presentation about writing C extensions for Ruby.
Paul Mucur
May 08, 2012
More Decks by Paul Mucur
See All by Paul Mucur
Do you really need breakfast?
mudge
0
140
HyperLogLog in 15 minutes
mudge
0
340
Exploring #to_proc
mudge
0
250
Testing Vimscript with Vimrunner
mudge
3
250
Configuration Management with Puppet
mudge
1
250
Managing Web Application Servers with Puppet
mudge
3
160
Other Decks in Programming
See All in Programming
Are We Really Coding 10× Faster with AI?
kohzas
0
170
20260514 - build with ai 2026 - build LINE Bot with Gemini CLI
line_developers_tw
PRO
0
440
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
390
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
460
実践ハーネスエンジニアリング:ステアリングループを実例から読み解く / Practical Harness Engineering: Understanding Steering Loops Through Real-World Examples
nrslib
5
5.4k
過去のレビュー知見をSkillsで資産化した話
pkshadeck
PRO
1
1.9k
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
130
Surviving Black Friday: 329 billion requests with Falcon!
ioquatix
0
3.1k
Kingdom of the Machine
yui_knk
2
1.5k
KMP × Kotlin 2.3 - How Android Got Slower While iOS Builds Improved by 47%
rio432
0
190
[RubyKaigi 2026] Require Hooks
palkan
1
320
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
350
Featured
See All Featured
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
44k
Faster Mobile Websites
deanohume
310
31k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
GraphQLとの向き合い方2022年版
quramy
50
15k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
370
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Building the Perfect Custom Keyboard
takai
2
750
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
140
Optimizing for Happiness
mojombo
378
71k
Transcript
EXTENDING RUBY with C
Why?
PERFORMANCE
PERFORMANCE
C BINDINGS
C BINDINGS FFI
None
None
array << 1
static VALUE rb_ary_push_1(VALUE ary, VALUE item) {
long idx = RARRAY_LEN(ary); if (idx >= ARY_CAPA(ary)) { ary_double_capa(ary, idx); } RARRAY_PTR(ary)[idx] = item; ARY_SET_LEN(ary, idx + 1); return ary; }
EXT/WORLD/WORLD.C
> World.hello => "Hello, World!"
require "world" describe World do describe "#hello" do
it "greets the world" do World.hello.should == "Hello, World!" end end end
module World module_function def hello
"Hello, World!" end end
None
HELLO
return rb_str_new("Hello, World!", 13);
static VALUE world_hello(VALUE obj) { return rb_str_new("Hello, World!", 13);
}
WORLD
VALUE world_mWorld = rb_define_module("World"); rb_define_module_function(world_mWorld, "hello", world_hello,
0);
#include <ruby.h> static VALUE world_hello(VALUE obj) { return rb_str_new("Hello,
World!", 13); } void Init_world(void) { VALUE world_mWorld = rb_define_module("World"); rb_define_module_function(world_mWorld, "hello", world_hello, 0); }
COMPILATION
require "mkmf"
require "mkmf" $CFLAGS << " -‐Wall -‐Wextra" create_makefile("world")
$ ruby ext/world/extconf.rb creating Makefile
$ make compiling ext/world/world.c linking shared-‐object world.bundle
$ rspec spec/world_spec.rb . Finished in 0.00399 seconds 1
example, 0 failures
None
$ rake compile
require 'rake/extensiontask' require 'rspec/core/rake_task' Rake::ExtensionTask.new('world') RSpec::Core::RakeTask.new('test') task :test
=> :compile task :default => :test
it "greets someone by name, if given" do World.hello("Bob").should
== "Hello, Bob!" end
“If argc is -1, the function will be called as:
VALUE func(int argc, VALUE *argv, VALUE obj) where argc is the actual number of arguments, argv is the C array of the arguments, and obj is the receiver.”
def hello(name = nil) if name
"Hello, #{name}!" else "Hello, World!" end end
rb_define_module_function(world_mWorld, "hello", world_hello, -‐1);
static VALUE world_hello(int argc, VALUE *argv, VALUE obj) {
VALUE name; rb_scan_args(argc, argv, "01", &name); ... }
static VALUE world_hello(int argc, VALUE *argv, VALUE obj) {
VALUE name; rb_scan_args(argc, argv, "01", &name); if (NIL_P(name)) { return rb_str_new("Hello, World!", 13); } else { ... } }
static VALUE world_hello(int argc, VALUE *argv, VALUE obj) {
VALUE name, greeting; rb_scan_args(argc, argv, "01", &name); if (NIL_P(name)) { greeting = rb_str_new("Hello, World!", 13); } else { greeting = rb_str_new("Hello, ", 7); rb_str_cat(greeting, RSTRING_PTR(name), RSTRING_LEN(name)); rb_str_cat(greeting, "!", 1); } return greeting; }
$ rake clean test rm -‐r tmp/x86_64-‐darwin11.3.0/world/1.9.3 mkdir -‐p tmp/x86_64-‐darwin11.3.0/world/1.9.3
cd tmp/x86_64-‐darwin11.3.0/world/1.9.3 /Users/mudge/.rbenv/versions/1.9.3-‐p125/bin/ruby -‐ I. ../../../../ext/world/extconf.rb creating Makefile cd -‐ cd tmp/x86_64-‐darwin11.3.0/world/1.9.3 make compiling ../../../../ext/world/world.c linking shared-‐object world.bundle cd -‐ install -‐c tmp/x86_64-‐darwin11.3.0/world/1.9.3/world.bundle lib/ world.bundle /Users/mudge/.rbenv/versions/1.9.3-‐p125/bin/ruby -‐S rspec ./ spec/world_spec.rb .. Finished in 0.00081 seconds 2 examples, 0 failures
None
QUESTIONS?