Ruby
Add Ruby runtime to your VMs.
The Ruby integration installs Ruby via RVM and provides helper methods for running Ruby code and installing gems.
Installation
npm install @freestyle-sh/with-ruby freestyle-sandboxesUsage
import { freestyle } from "freestyle-sandboxes";
import { VmRuby } from "@freestyle-sh/with-ruby";
const { vm } = await freestyle.vms.create({
with: {
ruby: new VmRuby(),
},
});
const res = await vm.ruby.runCode({
code: "require 'json'; puts JSON.generate({ hello: 'world' })"
});
console.log(res);
// { result: { hello: 'world' }, stdout: '{"hello":"world"}\n', statusCode: 0 }Options
new VmRuby({
version: "3.3.6", // Optional: specific Ruby version (default: "3.4.8")
})| Option | Type | Default | Description |
|---|---|---|---|
version | string | "3.4.8" | Ruby version to install via RVM |
API
vm.ruby.runCode({ code })
Executes Ruby code in the RVM-managed Ruby runtime.
const res = await vm.ruby.runCode({
code: `
require 'json'
data = { sum: 1 + 2, list: [1, 2, 3] }
puts JSON.generate(data)
`
});
console.log(res.result); // { sum: 3, list: [1, 2, 3] }Returns: Promise<RunCodeResponse>
type RunCodeResponse<Result> = {
result: Result; // Parsed JSON from stdout (if valid JSON)
stdout?: string; // Raw stdout output
stderr?: string; // Raw stderr output
statusCode?: number; // Exit code
};vm.ruby.install(options?)
Installs gems via gem install or bundle install.
// Install specific gems
await vm.ruby.install({ deps: ["nokogiri", "colorize"] });
// Install from Gemfile (bundle install)
await vm.ruby.install();
await vm.ruby.install({ directory: "/app" });Returns: Promise<InstallResult>
type InstallResult = {
success: boolean;
stdout?: string;
stderr?: string;
};Example: Run Code and Get Result
import { freestyle } from "freestyle-sandboxes";
import { VmRuby } from "@freestyle-sh/with-ruby";
const { vm } = await freestyle.vms.create({
with: {
ruby: new VmRuby({ version: "3.3.6" }),
},
});
const res = await vm.ruby.runCode({
code: `
require 'json'
numbers = [1, 2, 3, 4, 5]
result = {
sum: numbers.sum,
average: numbers.sum.to_f / numbers.length,
squared: numbers.map { |n| n ** 2 }
}
puts JSON.generate(result)
`
});
console.log(res.result);
// { sum: 15, average: 3.0, squared: [1, 4, 9, 16, 25] }