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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
240
Managing Web Application Servers with Puppet
mudge
3
150
Other Decks in Programming
See All in Programming
「話せることがない」を乗り越える 〜日常業務から登壇テーマをつくる思考法〜
shoheimitani
4
840
Terraform言語の静的解析 / static analysis of Terraform language
wata727
1
100
HTML-Aware ERB: The Path to Reactive Rendering @ RubyKaigi 2026, Hakodate, Japan
marcoroth
0
170
レガシーPHP転生 〜父がドメインエキスパートだったのでDDD+Claude Codeでチート開発します〜
panda_program
0
1k
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
3
280
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
5k
GNU Makeの使い方 / How to use GNU Make
kaityo256
PRO
16
5.6k
AI時代のPhpStorm最新事情 #phpcon_odawara
yusuke
0
190
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.6k
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
210
瑠璃の宝石に学ぶ技術の声の聴き方 / 【劇場版】アニメから得た学びを発表会2026 #エンジニアニメ
mazrean
0
270
2026_04_15_量子計算をパズルとして解く
hideakitakechi
0
110
Featured
See All Featured
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
180
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.2k
Amusing Abliteration
ianozsvald
1
160
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
480
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
140
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
160
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Site-Speed That Sticks
csswizardry
13
1.2k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
340
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
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?