Skip to content
Volume R · The TensorCore.si Library

Ruby

Optimised for programmer happiness.

Ruby is an elegant, expressive language best known for Ruby on Rails, the framework behind GitHub, Shopify and Basecamp. Its readable style makes it a joy for building web apps, and gems like ruby_llm bring AI into Rails apps.

Prologue

Why learn Ruby?

Ruby reads almost like English and lets you build working web products remarkably fast with Rails.

  • Readable: concise, expressive code with few symbols.
  • Rails: build full web apps quickly with strong conventions.
  • AI in Rails: gems like ruby_llm and langchainrb add chat, tools and RAG.
Prologue

Set up and run your first program

Install Ruby 3.3+ with a version manager such as rbenv, asdf or RubyInstaller on Windows.

terminal
ruby -v
ruby hello.rb
gem install rails
hello.rb
name = "Ada"
puts "Hello, AI! Welcome to Ruby, #{name}."
TipGems are Ruby packages; Bundler manages them per project with a Gemfile.
Chapter 1

Variables and types

No declarations needed. Strings interpolate with #{}. Symbols (:fp16) are lightweight names. Everything, even numbers, is an object with methods.

variables.rb
layers = 32
lr = 3e-4
model = "llama3.1"
params = 8_030_000_000
precision = :fp16
puts "#{model}: #{layers} layers, FP16 #{(params * 2 / 1e9).round(1)} GB"
puts 42.even?, precision.class
Chapter 2

Control flow and blocks

Ruby has if, unless, case/when and loops, but most iteration uses blocks: code passed to methods like each and times.

control.rb
3.times { |epoch| puts "epoch #{epoch}" }
%w[H100 B200].each_with_index do |gpu, i|
  puts "#{i}: #{gpu}"
end
bits = 8
name = case bits
       when 32 then "FP32"
       when 16 then "FP16"
       when 8 then "FP8"
       else "other"
       end
puts name unless bits > 16
Chapter 3

Arrays and hashes

Arrays and hashes come with dozens of methods such as select, map, sum and sort_by that take blocks.

collections.rb
vram = { "H100" => 80, "H200" => 141, "B200" => 180 }
big = vram.select { |_, gb| gb > 100 }.keys
puts big.inspect                    # ["H200", "B200"]
puts vram.values.sum / vram.size
puts vram.sort_by { |_, gb| -gb }.first.inspect
Chapter 4

Classes and modules

Define classes with class, attributes with attr_reader, and share behaviour with modules mixed in by include.

classes.rb
module Describable
  def describe = "#{name}: #{vram_gb} GB"
end
class Gpu
  include Describable
  attr_reader :name, :vram_gb
  def initialize(name, vram_gb)
    @name, @vram_gb = name, vram_gb
  end
  def fits?(gb) = gb <= vram_gb
end
g = Gpu.new("RTX 5090", 32)
puts g.describe, g.fits?(16)
Chapter 5

Errors and files

Handle errors with begin/rescue. File and JSON handling are built into the standard library.

errors.rb
require "json"
begin
  data = JSON.parse(File.read("config.json"))
  puts data["model"]
rescue Errno::ENOENT
  puts "config.json not found"
rescue JSON::ParserError => e
  puts "Bad JSON: #{e.message}"
end
Part II · AI with Ruby

AI with Ruby

Ruby is used to put AI into web products: chat features in Rails apps, document Q&A with pgvector, and background jobs that classify or summarise content using LLM APIs.

The essential libraries

ruby_llmOne clean API for chat, tools and embeddings across providers.
langchainrbChains, RAG and agents for Ruby.
neighborVector search with pgvector in Rails.
ruby-openaiA widely used OpenAI-compatible client.

Gradient descent from scratch

Learn y = 3x + 0.5 with plain Ruby.

gradient.rb
n = 100
xs = (0...n).map { |i| -1 + 2.0 * i / (n - 1) }
ys = xs.map { |x| 3 * x + 0.5 + (rand - 0.5) * 0.2 }
w = b = 0.0
lr = 0.1
500.times do
  gw = gb = 0.0
  xs.each_with_index do |x, i|
    err = w * x + b - ys[i]
    gw += 2 * err * x / n
    gb += 2 * err / n
  end
  w -= lr * gw
  b -= lr * gb
end
puts format("w=%.2f b=%.2f", w, b)
Part III · Build an AI agent

Build an AI agent in Ruby

The standard library's net/http and json are enough to run the tool-calling loop against a local model.

Your goal→Model plans→Tool call→Your code runs the tool→Result back to model→Final answer
agent.rb
require "net/http"
require "json"

URL = URI("http://localhost:11434/v1/chat/completions")
TOOLS = [{ type: "function", function: { name: "get_weather", description: "Weather for a city",
  parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] } } }]
def get_weather(city) = "It is 24°C and sunny in #{city}."

messages = [{ role: "user", content: "Should I take an umbrella in Lahore today?" }]
5.times do
  res = Net::HTTP.post(URL, { model: "llama3.1", messages: messages, tools: TOOLS }.to_json,
                       "Content-Type" => "application/json")
  msg = JSON.parse(res.body)["choices"][0]["message"]
  messages << msg
  calls = msg["tool_calls"] || []
  if calls.empty?
    puts msg["content"]
    break
  end
  calls.each do |call|
    args = JSON.parse(call["function"]["arguments"])
    messages << { role: "tool", tool_call_id: call["id"], content: get_weather(args["city"]) }
  end
end

The ruby_llm gem offers the same loop with tools defined as Ruby classes.

Run it locallyInstall Ollama, run ollama pull llama3.1, and keep it running. The code talks to its OpenAI-compatible API at http://localhost:11434/v1, so the same program also works with vLLM, llama.cpp server or a cloud API by changing the URL, model name and adding an API key.
Part IV · Your path

Ruby learning roadmap

Tick items off as you go. Your progress is saved in this browser.

Beginner

Intermediate

Advanced

Part IV · Your path

Projects to build

CLI to-do listStore tasks in a JSON file.
Rails blogPosts, comments and authentication.
AI summariserA Rails app that summarises articles with an LLM.
Reference

Ruby cheat sheet

TaskSyntax
Printputs "Hi #{name}"
Array[1, 2, 3].map { |x| x * 2 }
Hash{ a: 1, b: 2 }
Methoddef add(a, b) = a + b
Classclass P; attr_reader :x; end
Rescuebegin ... rescue => e ... end
Reference

Common mistakes

  • nil has methods too; calling a missing one raises NoMethodError, so check with &..
  • Mutating strings that are shared can surprise you; use .dup or frozen string literals.
  • Forgetting end is the most common syntax error.
Reference

Where to go next

On TensorCore.si: Python volume, PHP volume.

Keyboard: [ previous language · ] next language · / search the library