Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
Q

quartz

> 编程语言
Open source

A gem for calling Go code from within your Ruby code

532 stars0 likes3 views
WebsiteGitHub

About

A gem for calling Go code from within your Ruby code

quartz

Quartz enables you to call Go code from within your Ruby code. This is accomplished by running a Go program as a child process of your Ruby application and using UNIX domain sockets for communication.

With this gem, you can now write performance critical code in Go, and that code can be called from anywhere in a Ruby project.

To see some real world examples where Go can aid a Ruby project, please see the examples/ directory.

Defining exportable structs

Quartz shares Go code by exporting methods on a struct to Ruby.

Quartz requires that all arguments to exported struct methods be JSON serializable types. Additionally, the arguments to an exported method should be of the form (A, *B), where A and B are JSON serializable types. The method should also return an error. Here's an example of an exportable struct and method:

go
type Adder struct{}

type Args struct {
	A int
	B int
}

type Response struct {
	Sum int
}

func (t *Adder) Add(args Args, response *Response) error {
	*response = Response{args.A + args.B}
	return nil
}

For more information regarding Go's RPC rules, which must still be followed when using quartz, please examine the official documentation for the net/rpc package here.

Preparing a Quartz RPC server in Go

Instead of integrating Quartz into an existing Go application, it is recommended to create a new program that explicitly defines the structs that should be available to Ruby.

First, import the Go package:

go
import (
	"github.com/DavidHuie/quartz/go/quartz"
)

Quartz maintains most of Go's native RPC interface. Thus, export a struct by calling this in your main function:

go
myAdder := &Adder{}
quartz.RegisterName("my_adder", myAdder)

Once all structs have been declared, start the RPC server (this is a blocking call):

go
quartz.Start()

In Ruby

Naturally:

bash
$ gem install quartz

If you have a go run-able file, you can create a Go client that points to that file:

ruby
client = Quartz::Client.new(file_path: 'my_adder.go')

If you compiled the Go program yourself, you can create a client that points to the binary like this:

ruby
client = Quartz::Client.new(bin_path: 'my_adder_binary')

To list exported structs:

ruby
client.structs
=> [:my_adder]

To list a struct's exported methods:

ruby
client[:my_adder].struct_methods
=> ["Add"]

To call a method on a struct:

ruby
client[:my_adder].call('Add', 'A' => 2, 'B' => 5)
=> { 'Sum' => 7 }

By default the unix socket is created under /tmp. If you want to change that you can pass the socket_dir option.

ruby
client = Quartz::Client.new(bin_path: 'my_adder_binary', socket_dir: '/apps/my_app/tmp')

If you want to manage the Go process manually, you can use the following system to set the socket path manually. Now, you can use Systemd/Kubernetes/etc. to manage the Go process.

bash
$ QUARTZ_SOCKET=/tmp/custom.sock ./go_process
ruby
client = Quartz::Client.new(socket_path: '/tmp/custom.sock')

Copyright

Copyright (c) 2018 David Huie. See LICENSE.txt for further details.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言