Skip to content

Instantly share code, notes, and snippets.

@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 December 28, 2023 06:27
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'
@jaredcwhite
jaredcwhite / funky.config.js
Last active May 20, 2021 03:40
Personal configuration of Funky class-less utility CSS. https://native-elements.dev/#/docs/funky/introduction
module.exports = {
minify: false,
outputPath: "frontend/styles/funky-utilities.css",
breakpoints: {
xs: { max: "575px" },
sm: { min: "576px", max: "767px" },
md: { min: "768px", max: "991px" },
lg: { min: "992px", max: "1199px" },
xl: { min: "1200px", max: "1399px" },
xxl: "1400px"