
Security News
Astral Launches pyx: A Python-Native Package Registry
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
claude-code-sdk-ruby
Advanced tools
Official Ruby SDK for interacting with Claude Code CLI. This gem provides a Ruby-idiomatic interface to Claude Code with full async support, proper error handling, and comprehensive type definitions.
Add this line to your application's Gemfile:
gem 'claude-code-sdk-ruby'
And then execute:
bundle install
Or install it yourself as:
gem install claude-code-sdk-ruby
This SDK requires the Claude Code CLI to be installed:
npm install -g @anthropic-ai/claude-code
You'll also need to configure your API key. Please refer to the Claude Code documentation for detailed setup instructions.
require 'claude_sdk'
# Simple query
ClaudeSDK.query("What is 2+2?") do |message|
case message
when ClaudeSDK::Messages::Assistant
message.content.each do |block|
puts block.text if block.is_a?(ClaudeSDK::ContentBlock::Text)
end
when ClaudeSDK::Messages::Result
puts "Session completed in #{message.duration_ms}ms"
end
end
require 'claude_sdk'
ClaudeSDK.query("Hello, Claude!") do |message|
puts message
end
require 'claude_sdk'
options = ClaudeSDK::ClaudeCodeOptions.new(
allowed_tools: ['Read', 'Write', 'Bash'],
max_turns: 3,
system_prompt: 'You are a helpful coding assistant.',
cwd: '/path/to/working/directory'
)
ClaudeSDK.query("Help me write a Ruby script", options: options) do |message|
case message
when ClaudeSDK::Messages::Assistant
message.content.each do |block|
case block
when ClaudeSDK::ContentBlock::Text
puts block.text
when ClaudeSDK::ContentBlock::ToolUse
puts "Tool: #{block.name}"
puts "Input: #{block.input}"
end
end
when ClaudeSDK::Messages::Result
puts "Completed in #{message.num_turns} turns"
puts "Total cost: $#{message.total_cost_usd}"
end
end
require 'claude_sdk'
messages = ClaudeSDK.query("Hello")
messages.each do |message|
puts message
end
You can load additional settings from a JSON file:
require 'claude_sdk'
# Create options with a settings file path
options = ClaudeSDK::ClaudeCodeOptions.new(
settings: '/path/to/claude-settings.json',
model: 'sonnet' # Other options can still be specified
)
ClaudeSDK.query("Hello", options: options) do |message|
puts message
end
The settings file should be a valid JSON file containing Claude Code configuration options:
{
"permissions": {
"allow": [
"Bash(npm run lint)",
"Bash(npm run test:*)",
"Read(~/.zshrc)"
],
"deny": [
"Bash(curl:*)"
]
},
"env": {
"CLAUDE_CODE_ENABLE_TELEMETRY": "1",
"OTEL_METRICS_EXPORTER": "otlp"
},
"model": "claude-3-5-sonnet-20241022",
"cleanupPeriodDays": 20,
"includeCoAuthoredBy": true
}
require 'claude_sdk/internal/client'
client = ClaudeSDK::Internal::InternalClient.new
options = ClaudeSDK::ClaudeCodeOptions.new(
allowed_tools: ['Read', 'Bash'],
max_turns: 5
)
Async do
client.process_query(prompt: "What files are in this directory?", options: options) do |message|
case message
when ClaudeSDK::Messages::Assistant
puts "Assistant response received"
when ClaudeSDK::Messages::System
puts "System message: #{message.subtype}"
when ClaudeSDK::Messages::Result
puts "Query completed"
end
end
end
The SDK provides several message types:
ClaudeSDK::Messages::User
- User input messagesClaudeSDK::Messages::Assistant
- Assistant responses containing content blocksClaudeSDK::Messages::System
- System messages (tool results, errors)ClaudeSDK::Messages::Result
- Query completion results with timing and cost infoClaudeSDK::Messages::Error
- Error messages from the CLIAssistant messages contain content blocks:
ClaudeSDK::ContentBlock::Text
- Text contentClaudeSDK::ContentBlock::ToolUse
- Tool usageClaudeSDK::ContentBlock::ToolResult
- Tool resultsThe ClaudeCodeOptions
class supports all Claude Code CLI options:
allowed_tools
- Array of allowed tool names (e.g., ['Read', 'Write', 'Bash']
)disallowed_tools
- Array of disallowed tool namesmax_turns
- Maximum conversation turnssystem_prompt
- System prompt for the conversationappend_system_prompt
- Additional system prompt to appendcwd
- Working directory for file operationsmodel
- Model to use (e.g., 'claude-3-opus'
)permission_mode
- Permission mode (:default
, :accept_edits
, :auto
, :ask
)permission_prompt_tool_name
- Tool for permission promptscontinue_conversation
- Whether to continue from previous conversationresume
- Resume from a specific conversation IDmcp_servers
- MCP server configurationsettings
- Path to a settings JSON file to load additional settings fromThe SDK provides specific error types:
begin
ClaudeSDK.query("Hello") do |message|
puts message
end
rescue ClaudeSDK::CLINotFoundError => e
puts "Claude Code CLI not found: #{e.message}"
rescue ClaudeSDK::CLIConnectionError => e
puts "Connection error: #{e.message}"
rescue ClaudeSDK::ProcessError => e
puts "Process error: #{e.message}"
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
# Run tests
bundle exec rspec
# Run tests with coverage
COVERAGE=true bundle exec rspec
# Build the gem
gem build claude-code-sdk-ruby.gemspec
# Install locally
gem install ./claude-code-sdk-ruby-0.1.0.gem
To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/parruda/claude-code-sdk-ruby.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that claude-code-sdk-ruby demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.