Skip to content

Instantly share code, notes, and snippets.

class BaseClient
class APINotFound < StandardError; end
attr_reader :auth_strategy
def initialize(auth_strategy: :headers, headers: {})
@auth_strategy = auth_strategy
@headers = headers
end
@tenderlove
tenderlove / mdns.rb
Created November 19, 2023 20:02
Sample mDNS client in Ruby
# mDNS client
#
# Usage: ruby script.rb "_http._tcp.local."
require "socket"
require "ipaddr"
require "fcntl"
require "resolv"
module DNSSD
@peterc
peterc / embedding_store.rb
Last active February 15, 2025 12:33
Using SQLite to store OpenAI vector embeddings from Ruby
# Example of using SQLite VSS with OpenAI's text embedding API
# from Ruby.
# Note: Install/bundle the sqlite3, sqlite_vss, and ruby-openai gems first
# OPENAI_API_KEY must also be set in the environment
# Other embeddings can be used, but this is the easiest for a quick demo
# More on the topic at
# https://observablehq.com/@asg017/introducing-sqlite-vss
# https://observablehq.com/@asg017/making-sqlite-extension-gem-installable
@kddnewton
kddnewton / latexify.rb
Last active November 7, 2022 14:45
LaTeXify Ruby methods
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "syntax_tree"
end
require 'thread'
require 'logger'
POOL_SIZE = 10 # limit for HTTP parallel connections
QUEUE_SIZE = 5 # limit to consume only fixed amount of memory
queue = SizedQueue.new(QUEUE_SIZE)
logger = Logger.new($stdout)
END_MESSAGE = :done
ERROR_MESSAGE = :error
# frozen_string_literal: true
source 'https://rubygems.org'
gem 'rack', github: 'rack/rack'
gem 'puma', github: 'puma/puma'
# Rails production setup via SQLite3 made durable by https://litestream.io/
# Copy this to Dockerfile on a fresh rails app. Deploy to fly.io or any other container engine.
#
# try locally: docker build . -t rails && docker run -p3000:3000 -it rails
#
# in production you might want to map /data to somewhere on the host,
# but you don't have to!
#
FROM ruby:3.0.2
@havenwood
havenwood / async.rb
Last active February 12, 2022 17:43
require 'async'
class Enumerator
class Async
module Refinement
refine Enumerable do
def async
Enumerator::Async.new(self)
end
end
@havenwood
havenwood / task.rb
Last active February 12, 2022 17:42
Just a overly simplified example of calling methods without arguments concurrently with Async and in parallel with Ractors
require 'async'
module Task
module_function
def async(*method_names)
Async do
method_names.map do |job|
Async do
job.to_proc.call(self)
@palkan
palkan / frame_display.rb
Last active November 2, 2021 15:32
tcpdump -> pretty http2 packets
# frozen_string_literal: true
# Example usage:
#
# tcpdump -i lo0 'port 3010' -v -X -l | ruby frame_display.rb
#
require 'bundler/inline'
gemfile(true, quiet: true) do
source 'https://rubygems.org'