Skip to content

Instantly share code, notes, and snippets.

@sakuro
Last active December 28, 2025 12:34
Show Gist options
  • Select an option

  • Save sakuro/98d670e6fdc1f1d906ebef00ed060913 to your computer and use it in GitHub Desktop.

Select an option

Save sakuro/98d670e6fdc1f1d906ebef00ed060913 to your computer and use it in GitHub Desktop.
Diff of `bundle gem` without/with --ext=rust
diff -urN foo1/.github/workflows/main.yml foo2/.github/workflows/main.yml
--- foo1/.github/workflows/main.yml 2025-12-28 21:30:58.265960275 +0900
+++ foo2/.github/workflows/main.yml 2025-12-28 21:31:06.044305950 +0900
@@ -20,10 +20,12 @@
- uses: actions/checkout@v4
with:
persist-credentials: false
- - name: Set up Ruby
- uses: ruby/setup-ruby@v1
+ - name: Set up Ruby & Rust
+ uses: oxidize-rb/actions/setup-ruby-and-rust@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
+ cargo-cache: true
+ rubygems: '4.0.3'
- name: Run the default task
run: bundle exec rake
diff -urN foo1/.gitignore foo2/.gitignore
--- foo1/.gitignore 2025-12-28 21:30:58.264777528 +0900
+++ foo2/.gitignore 2025-12-28 21:31:06.043155369 +0900
@@ -6,6 +6,12 @@
/pkg/
/spec/reports/
/tmp/
+*.bundle
+*.so
+*.o
+*.a
+mkmf.log
+target/
# rspec failure tracking
.rspec_status
diff -urN foo1/Cargo.toml foo2/Cargo.toml
--- foo1/Cargo.toml 1970-01-01 09:00:00.000000000 +0900
+++ foo2/Cargo.toml 2025-12-28 21:31:06.045794863 +0900
@@ -0,0 +1,7 @@
+# This Cargo.toml is here to let externals tools (IDEs, etc.) know that this is
+# a Rust project. Your extensions dependencies should be added to the Cargo.toml
+# in the ext/ directory.
+
+[workspace]
+members = ["./ext/foo"]
+resolver = "2"
diff -urN foo1/Gemfile foo2/Gemfile
--- foo1/Gemfile 2025-12-28 21:30:58.260349537 +0900
+++ foo2/Gemfile 2025-12-28 21:31:06.040039834 +0900
@@ -8,6 +8,8 @@
gem "irb"
gem "rake", "~> 13.0"
+gem "rake-compiler"
+
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
diff -urN foo1/Rakefile foo2/Rakefile
--- foo1/Rakefile 2025-12-28 21:30:58.263099448 +0900
+++ foo2/Rakefile 2025-12-28 21:31:06.042149496 +0900
@@ -9,4 +9,14 @@
RuboCop::RakeTask.new
-task default: %i[spec rubocop]
+require "rb_sys/extensiontask"
+
+task build: :compile
+
+GEMSPEC = Gem::Specification.load("foo.gemspec")
+
+RbSys::ExtensionTask.new("foo", GEMSPEC) do |ext|
+ ext.lib_dir = "lib/foo"
+end
+
+task default: %i[compile spec rubocop]
diff -urN foo1/ext/foo/Cargo.toml foo2/ext/foo/Cargo.toml
--- foo1/ext/foo/Cargo.toml 1970-01-01 09:00:00.000000000 +0900
+++ foo2/ext/foo/Cargo.toml 2025-12-28 21:31:06.046661403 +0900
@@ -0,0 +1,13 @@
+[package]
+name = "foo"
+version = "0.1.0"
+edition = "2021"
+authors = ["OZAWA Sakuro <10973+sakuro@users.noreply.github.com>"]
+license = "MIT"
+publish = false
+
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies]
+magnus = { version = "0.8.2" }
diff -urN foo1/ext/foo/extconf.rb foo2/ext/foo/extconf.rb
--- foo1/ext/foo/extconf.rb 1970-01-01 09:00:00.000000000 +0900
+++ foo2/ext/foo/extconf.rb 2025-12-28 21:31:06.047004986 +0900
@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+require "mkmf"
+require "rb_sys/mkmf"
+
+create_rust_makefile("foo/foo")
diff -urN foo1/ext/foo/src/lib.rs foo2/ext/foo/src/lib.rs
--- foo1/ext/foo/src/lib.rs 1970-01-01 09:00:00.000000000 +0900
+++ foo2/ext/foo/src/lib.rs 2025-12-28 21:31:06.047556901 +0900
@@ -0,0 +1,12 @@
+use magnus::{function, prelude::*, Error, Ruby};
+
+fn hello(subject: String) -> String {
+ format!("Hello from Rust, {subject}!")
+}
+
+#[magnus::init]
+fn init(ruby: &Ruby) -> Result<(), Error> {
+ let module = ruby.define_module("Foo")?;
+ module.define_singleton_method("hello", function!(hello, 1))?;
+ Ok(())
+}
diff -urN foo1/foo.gemspec foo2/foo.gemspec
--- foo1/foo.gemspec 2025-12-28 21:30:58.262631532 +0900
+++ foo2/foo.gemspec 2025-12-28 21:31:06.041724705 +0900
@@ -13,6 +13,7 @@
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"
spec.required_ruby_version = ">= 3.2.0"
+ spec.required_rubygems_version = ">= 3.3.11"
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
spec.metadata["homepage_uri"] = spec.homepage
@@ -31,9 +32,11 @@
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
+ spec.extensions = ["ext/foo/extconf.rb"]
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
+ spec.add_dependency "rb_sys", "~> 0.9.91"
# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
diff -urN foo1/lib/foo.rb foo2/lib/foo.rb
--- foo1/lib/foo.rb 2025-12-28 21:30:58.261082660 +0900
+++ foo2/lib/foo.rb 2025-12-28 21:31:06.040521583 +0900
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require_relative "foo/version"
+require "foo/foo"
module Foo
class Error < StandardError; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment