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
Paul Mucur
May 08, 2012
Programming
1
290
Extending Ruby with C
A Learning Lunch presentation about writing C extensions for Ruby.
Paul Mucur
May 08, 2012
Tweet
Share
More Decks by Paul Mucur
See All by Paul Mucur
Do you really need breakfast?
mudge
0
140
HyperLogLog in 15 minutes
mudge
0
330
Exploring #to_proc
mudge
0
240
Testing Vimscript with Vimrunner
mudge
3
240
Configuration Management with Puppet
mudge
1
240
Managing Web Application Servers with Puppet
mudge
3
150
Other Decks in Programming
See All in Programming
モビリティSaaSにおけるデータ利活用の発展
nealle
0
580
AI駆動開発ライフサイクル(AI-DLC)のホワイトペーパーを解説
swxhariu5
0
1.3k
最新のDirectX12で使えるレイトレ周りの機能追加について
projectasura
0
290
AI時代もSEOを頑張っている話
shirahama_x
0
160
Private APIの呼び出し方
kishikawakatsumi
3
900
高単価案件で働くための心構え
nullnull
0
160
開発生産性が組織文化になるまでの軌跡
tonegawa07
0
190
Micro Frontendsで築いた 共通基盤と運用の試行錯誤 / Building a Shared Platform with Micro Frontends: Operational Learnings
kyntk
0
120
JJUG CCC 2025 Fall: Virtual Thread Deep Dive
ternbusty
3
480
「AWS CDK入門」の前日譚/Prequelto-Introduction-To-AWSCDK
tyumugi1113
0
100
知られているようで知られていない JavaScriptの仕様 4選
syumai
0
630
例外処理を理解して、設計段階からエラーを見つけやすく、起こりにくく #phpconfuk
kajitack
12
6.4k
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.8k
Done Done
chrislema
186
16k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
38
2.9k
Agile that works and the tools we love
rasmusluckow
331
21k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.6k
Scaling GitHub
holman
464
140k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.8k
A better future with KSS
kneath
239
18k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.1k
Building Applications with DynamoDB
mza
96
6.8k
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?