Skip to content

Instantly share code, notes, and snippets.

View Yegorov's full-sized avatar
🏢
Work it harder. Make it better. Do it faster.

Artem Yegorov

🏢
Work it harder. Make it better. Do it faster.
View GitHub Profile
@joeldrapper
joeldrapper / fuzzy_index.rb
Created March 27, 2025 11:20
Simple fuzzy index with left weight
class FuzzyIndex
def initialize
@index = Hash.new { |h, k| h[k] = Set.new }
end
def []=(key, value)
trigrams(key).each { @index[it] << [key, value] }
end
def [](query)
@lazaronixon
lazaronixon / _form.html.erb
Last active April 19, 2025 13:32
Hotwire Event-Driven Update Pattern
<%= form_with model: citizen, class: "card flex flex-col gap", data: { controller: "form" } do |form| %>
<div class="flex flex-col gap mb-2">
<div class="flex flex-col gap-half">
<% countries = Country.order(:name) %>
<%= label_tag :country_id, "Country", class: "text-sm font-medium leading-none" %>
<%= select_tag :country_id, options_from_collection_for_select(countries, :id, :name, citizen.country_id), include_blank: "Select one", class: "input", data: { action: "form#submit", form_submitter_param: "on_country_change" } %>
</div>
<div class="flex flex-col gap-half">
<% states = State.where(country_id: citizen.country_id).order(:name) %>
@jwbee
jwbee / jq.md
Last active April 24, 2025 13:19
Make Ubuntu packages 90% faster by rebuilding them

Make Ubuntu packages 90% faster by rebuilding them

TL;DR

You can take the same source code package that Ubuntu uses to build jq, compile it again, and realize 90% better performance.

Setting

I use jq for processing GeoJSON files and other open data offered in JSON format. Today I am working with a 500MB GeoJSON file that contains the Alameda County Assessor's parcel map. I want to run a query that prints the city for every parcel worth more than a threshold amount. The program is

@peterc
peterc / CONVENTIONS.md
Last active April 23, 2025 06:55
CONVENTIONS.md file for AI Rails 8 development
  • You MUST NOT try and generate a Rails app from scratch on your own by generating each file. For a NEW app you MUST use rails new first to generate all of the boilerplate files necessary.
  • Create an app in the current directory with rails new .
  • Use Tailwind CSS for styling. Use --css tailwind as an option on the rails new call to do this automatically.
  • Use Ruby 3.2+ and Rails 8.0+ practices.
  • Use the default Minitest approach for testing, do not use RSpec.
  • Default to using SQLite in development. rails new will do this automatically but take care if you write any custom SQL that it is SQLite compatible.
  • An app can be built with a devcontainer such as rails new myapp --devcontainer but only do this if requested directly.
  • Rails apps have a lot of directories to consider, such as app, config, db, etc.
  • Adhere to MVC conventions: singular model names (e.g., Product) map to plural tables (products); controllers are plural.
  • Guard against incapable browsers accessing controllers with `allo
# config/initializers/phlex_template_handler.rb
require "action_view"
require "phlex"
# Intercept unknown "capitalized" method calls (e.g., PageView(...)) in templates,
# look up a Phlex component class, instantiate it, and render it.
# Crucially, we re-bind the user’s block so that `self` is the component, not the ActionView context.
module PhlexDynamicMethodCalls
def method_missing(name, *args, **kwargs, &block)
# Only intercept method calls starting with an uppercase letter (e.g. "PageView", "MyComponent", etc.)
@peterc
peterc / sinatra-react.md
Last active February 20, 2025 18:20
How to set up a basic Sinatra + React webapp

How to set up a basic Sinatra + React webapp in 2025

Let's say you want to use Ruby for the backend of a basic webapp but React on the frontend. Here's how.

(Note: All tested on January 13, 2025 with Ruby 3.3, Sinatra 4.1.1, and React 18.3. Configs may change over time.)

First, create the app folder and set up Sinatra:

mkdir my-sinatra-react-app

Implementing Advisory Locks in SQLite3

In my Rails model, I initially had a function to handle advisory locks in PostgreSQL. Here's the code, which works as expected:

# Postgresql version
def with_advisory_lock
  self.class.connection.execute("SELECT pg_advisory_lock(#{self.id})")
  yield
@floehopper
floehopper / download
Created November 16, 2024 10:52
Download file using multiple parallel Range requests
#!/usr/bin/env ruby
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "typhoeus"
end
require "uri"
@kyrylo
kyrylo / colorized_logger.rb
Last active January 8, 2025 07:07
Nice colorized logs for Rails apps! With this initializer, you can instantly colorize your Rails development logs. Just copy and paste the code, and it’ll work. https://x.com/kyrylosilin/status/1852308566201237815
# frozen_string_literal: true
# config/initializers/colorized_logger.rb
# This initializer adds color to the Rails logger output. It's a nice way to
# visually distinguish log levels.
module ColorizedLogger
COLOR_CODES = {
debug: "\e[36m", # Cyan
info: "\e[32m", # Green
warn: "\e[33m", # Yellow
@mackuba
mackuba / follow_hashtag.rb
Created October 24, 2024 01:18
Script for following hashtags for my hashtag feed
#!/usr/bin/env ruby
# make sure you have Ruby installed, should be installed by default on macOS & Linux
# install Minisky lib with: [sudo] gem install minisky
# then run with: ruby follow_hashtag.rb somehashtag
require 'minisky'
if !File.exist?('config.yml')
puts "Create a config.yml file with contents like this:"