Skip to content

Instantly share code, notes, and snippets.

View peterc's full-sized avatar
🏠
Working from home

Peter Cooper peterc

🏠
Working from home
View GitHub Profile
@peterc
peterc / macos_app.rb
Created February 4, 2026 20:34
A native macOS app written in Ruby using FFI to call libobjc, etc.
#!/usr/bin/env ruby
# A macOS GUI app written in Ruby, calling libobjc directly via Fiddle.
# No gems. No Objective-C. Just Ruby and the runtime.
require "fiddle"
OBJC = Fiddle.dlopen("/usr/lib/libobjc.A.dylib")
APPKIT = Fiddle.dlopen("/System/Library/Frameworks/AppKit.framework/AppKit")
# Core runtime functions
@peterc
peterc / helloworld.c
Created February 4, 2026 20:26
A macOS app entirely in plain C
// A macOS GUI app written in pure C, using the Objective-C runtime directly.
// No Objective-C syntax anywhere. Just raw objc_msgSend and runtime calls.
// This is cursed. Enjoy.
// Compile with clang -Wall -Wextra -o hello_runtime hello_runtime.c \
// -framework Cocoa -lobjc
#include <objc/objc.h>
#include <objc/message.h>
#include <objc/runtime.h>
@peterc
peterc / image.rb
Created November 17, 2025 15:41
Produce images with gemini-2.5-flash-image (Nano Banana) with RubyLLM 1.9.1
# Just because the example in the docs doesn't seem to work outside of a Rails app
require 'ruby_llm'
RubyLLM.configure do |config|
config.gemini_api_key = ENV['GEMINI_API_KEY']
end
chat = RubyLLM.chat(model: "gemini-2.5-flash-image")
reply = chat.ask("Cyberpunk city street at night, raining, neon signs")
@peterc
peterc / openai.rb
Last active August 8, 2025 16:49
Calling GPT-5 from a plain Ruby script using the official OpenAI library
require "openai"
client = OpenAI::Client.new # assumes valid key in OPENAI_API_KEY
begin
resp = client.chat.completions.create(
model: :"gpt-5-nano",
reasoning_effort: :high,
verbosity: :low,
messages: [{ role: "user", content: "Tell me a joke" }]
@peterc
peterc / make_palette.py
Created August 2, 2025 16:25
Generate color palettes by generating AI images and extracting the colors
import sys
import requests
import replicate
from Pylette import extract_colors
# YOU NEED A REPLICATE API TOKEN IN 'REPLICATE_API_TOKEN'
prompt = sys.argv[1] if len(sys.argv) > 1 else "sandy beach"
output = replicate.run(
@peterc
peterc / openai-openrouter-multiple-models.rb
Last active December 10, 2025 10:52
How to call an OpenRouter LLM with the official Ruby OpenAI gem
require "openai"
client = OpenAI::Client.new(
api_key: ENV.fetch("OPENROUTER_API_KEY"),
base_url: "https://openrouter.ai/api/v1"
)
models = %w{
google/gemma-2b-it
anthropic/claude-sonnet-4
@peterc
peterc / useful.md
Last active July 7, 2025 09:20
Useful random stuff
@peterc
peterc / count_min.rb
Created May 4, 2025 13:23
A memory efficient count-min style compact approximator in Ruby
require 'zlib'
require 'objspace'
# A variant of https://en.wikipedia.org/wiki/Count%E2%80%93min_sketch
class CompactApproximator
attr_accessor :cells
def initialize(size, hashes, min_elem, join_fn, meet_fn)
@m, @k = size, hashes
@peterc
peterc / zig.txt
Created April 14, 2025 18:18
Basic Zig programming cheat sheet for LLM AI agent
# Zig Programming Cheat Sheet
This guide condenses the key ideas from Chapter 1 of the Zig book, emphasizing Zig's syntax, semantics, workflows, and philosophy. It distills nuanced language details, technical rules, compiler constraints, and examples for direct application by AI or technical users.
---
## 1. Zig Philosophy and Paradigm
- Zig is a **low-level, general-purpose, modern programming language** designed for safety, control, and simplicity.
- Major design goal: **Less is more** (removes confusing/unsafe C/C++ behaviors; adds consistency).
@peterc
peterc / README.md
Created April 14, 2025 17:41
GPT-4.1-mini generated documentation for Ruby's JSON library

JSON Library for Ruby

The JSON library is a comprehensive and robust solution for parsing, generating, and manipulating JSON data in Ruby. It offers full support for JSON encoding and decoding, including extensions for common Ruby types like Date, Time, Complex, Rational, and more. The library also provides advanced features such as extended JSON object creation, custom generators, and parser configurations.

This README provides an overview of usage, advanced options, compatible Ruby types, and important notes for developers who want to integrate JSON handling seamlessly in their Ruby applications.


Features