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
@michelson
michelson / async_method_job.rb
Created April 23, 2024 01:50
Asynchronously Concern
class AsyncMethodJob < ApplicationJob
queue_as :default
def perform(target:, method_name:, args:, queue_name: :default)
self.class.queue_as(queue_name)
# `target` could be either an instance or a class
target = target.constantize if target.is_a?(String) # Convert class name to class object if needed
target.send(method_name, *args)
end
end
@owaiswiz
owaiswiz / print_redundant_indexes.rb
Last active April 23, 2024 16:36
Print redundant indexes in a Rails app.
# Unnecessary indexes slows down writes and consumes additional storage and memory.
# Just paste this snippet in your Rails console (bundle exec rails c).
# And it will print all redundant indexes that are already covered by another index on the table:
# Table `pages`: index `site_idx` (site_id) already covered by `site_slug_idx` (site_id,slug)
# Table `optins`: index `list_idx` (list_id) already covered by `list_active_idx` (list_id,active)
ActiveRecord::Base.connection.tables.map do |table|
indexes = ActiveRecord::Base.connection.indexes(table).select(&:valid).reject(&:where)
@searls
searls / ensures_logger_broadcasts_to_stdout.rb
Last active March 4, 2024 19:18
This logger wrapper I wrote.
# Leverages the BroadcastLogger introduced in Rails 7.1 to wrap the current
# logger in a new logger that broadcasts to both the current logger and $stdout
#
# (Announcement: https://rubyonrails.org/2023/9/29/this-week-in-rails)
#
# If the current logger already broadcasts to $stdout, it will not be wrapped,
# making it safe to call this method multiple times without knowing the current
# logging "sitch".
#
# Usage probably looks something like this:
@adtac
adtac / Dockerfile
Last active April 13, 2024 22:33
#!/usr/bin/env docker run
#!/usr/bin/env -S bash -c "docker run -p 8080:8080 -it --rm \$(docker build --progress plain -f \$0 . 2>&1 | tee /dev/stderr | grep -oP 'sha256:[0-9a-f]*')"
# syntax = docker/dockerfile:1.4.0
FROM node:20
WORKDIR /root
RUN npm install sqlite3
@YOU54F
YOU54F / ruby_packagers.md
Last active December 18, 2023 20:22
State of Ruby packaging / executables - 2023

So you've got a cool app in Ruby, and you want to share it with your friends, but they don't love Ruby as much as you.

What do you do? Package it up along with the Ruby runtime and give it to them. Simple right? Maybe not so.

Thankfully smarter cookies than me, have tackled the problem, here is a map of the current state of play

  • Traveling-ruby
    • revisted for Ruby 2.6.10 -> 3.3.0-preview1 and arm64 MacOS/Windows in this fork
      • PR proposed upstream here
  • Rb2exe revisted to use the updated traveling-ruby releases in this fork
@jnunemaker
jnunemaker / levenshtein.sql
Created September 11, 2023 13:02
Some example usage of levenshtein to calculate the difference between two strings.
-- https://www.postgresql.org/docs/current/fuzzystrmatch.html#id-1.11.7.26.7
-- Calculates the distance between two strings.
SELECT levenshtein('New York', 'New York'); -- 0
SELECT levenshtein('New York', 'New Jersey'); -- 5
SELECT levenshtein('New York', 'Dallas'); -- 8
-- find the opponent with the name closest to 'New York'
SELECT * FROM opponents WHERE team_id = 1
ORDER BY levenshtein(name, 'New York')
# requires :password_reset_token and :password_reset_token_expires_at fields
module PasswordResetable
extend ActiveSupport::Concern
included do
before_validation do
if password_reset_token_expires_at && password_reset_expires_at.past?
expire_password_reset!
end
end
@flickgradley
flickgradley / data_diff.rb
Created August 6, 2023 03:58
#diff method for Ruby Data classes
Measure = Data.define(:amount, :unit) do
def diff(other)
raise ArgumentError.new("other must be same class") unless other.is_a?(self.class)
members.inject({}) do |memo, key|
thisVal, otherVal = send(key), other.send(key)
memo[key] = [thisVal, otherVal] unless thisVal == otherVal
memo
end