Skip to content

Instantly share code, notes, and snippets.

Avatar

Vladimir Dementyev palkan

View GitHub Profile
@palkan
palkan / stdout.rb
Created Aug 29, 2022
Yabeda stdout adapter
View stdout.rb
# frozen_string_literal: true
require "yabeda/base_adapter"
module Yabeda
module STDOUT
class Adapter < BaseAdapter
def register_counter!(_metric)
end
@palkan
palkan / example.rb
Created Aug 16, 2022
pattern_matching_joe.rb
View example.rb
require 'active_support/core_ext/hash/indifferent_access'
def send_admin_notification(changes = previous_changes)
case changes
in ends_at: [Time, _]
:resubscribed
in ends_at: [nil, Time]
:churned
in processor_plan: [nil, String]
:subscribed
@palkan
palkan / mailers_preview_spec.rb
Created Jul 8, 2022
mailers_preview_spec.rb
View mailers_preview_spec.rb
# frozen_string_literal: true
require "rails_helper"
# Automatically smoke-test all mailer previews
describe "/rails/mailers" do
subject { response }
ActionMailer::Preview.all.each do |preview|
next if preview.emails.empty?
View 01_refinement-benchmark.rb
# frozen_string_literal: true
require "benchmark_driver"
source = <<~RUBY
class Hash
def symbolize_keys
transform_keys { |key| key.to_sym rescue key }
end
@palkan
palkan / README.md
Created Jan 24, 2022
Rails boot time profiling
View README.md

Add the following to application.rb:

$icallbacks = []
$icallbacks.define_singleton_method(:print) do
  puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a}" }.join("\n")
end

ActiveSupport::Notifications.subscribe("load_config_initializer.railties") do |event|
 $icallbacks &lt;&lt; [event.payload[:initializer], event.duration]
View count_const_refs.rb
class Array
def deconstruct
self
end
end
class Hash
def deconstruct_keys(_)
self
end
@palkan
palkan / normalize.rb
Created Dec 24, 2021
ActiveModel .normalize
View normalize.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true, quiet: true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "activesupport", "~> 7.0.0", require: false
@palkan
palkan / frame_display.rb
Last active Nov 2, 2021
tcpdump -> pretty http2 packets
View frame_display.rb
# 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'
@palkan
palkan / bulletify.rb
Created Sep 7, 2020
Bulletify: RSpec helpers to run Bullet in tests
View bulletify.rb
RSpec.shared_context "bullet" do
before(:each) do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true # raise an error if N+1 query occurs
Bullet.start_request
end
after(:each) do
Bullet.perform_out_of_channel_notifications if Bullet.notification?
@palkan
palkan / compile.c
Created Jun 28, 2020
compile.c ADD_STACKPRINT
View compile.c
#define ADD_STACKPRINT(label, depth) do {\
ADD_INSN1(ret, line, putobject, rb_fstring_lit(label));\
ADD_SEND(ret, line, rb_intern("putme"), INT2FIX(0));\
ADD_INSN(ret, line, pop);\
for (int x = 0; x < depth ; x++ ){\
ADD_INSN1(ret, line, topn, INT2FIX(x));\
ADD_SEND(ret, line, rb_intern("putme"), INT2FIX(0));\
ADD_INSN(ret, line, pop);\
}\
} while(0)