Skip to content

Instantly share code, notes, and snippets.

View palkan's full-sized avatar

Vladimir Dementyev palkan

View GitHub Profile
@palkan
palkan / compile.c
Created June 28, 2020 08:01
compile.c ADD_STACKPRINT
#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)
@palkan
palkan / bulletify.rb
Created September 7, 2020 17:00
Bulletify: RSpec helpers to run Bullet in tests
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 / 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'
@palkan
palkan / normalize.rb
Created December 24, 2021 14:47
ActiveModel .normalize
# 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
class Array
def deconstruct
self
end
end
class Hash
def deconstruct_keys(_)
self
end
@palkan
palkan / README.md
Created January 24, 2022 13:29
Rails boot time profiling

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]
# 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 / mailers_preview_spec.rb
Created July 8, 2022 16:15
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?
@palkan
palkan / example.rb
Created August 16, 2022 19:33
pattern_matching_joe.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 / stdout.rb
Created August 29, 2022 22:03
Yabeda stdout adapter
# frozen_string_literal: true
require "yabeda/base_adapter"
module Yabeda
module STDOUT
class Adapter < BaseAdapter
def register_counter!(_metric)
end