Skip to content

Instantly share code, notes, and snippets.

How to run examples

  1. Run $ createdb uniq-db-test to create DB
  2. Run example with Ruby (e.g., $ ruby 1_find_or_create_by_single_thread.rb)

Benchmark output

With many successful INSERTs

Warming up --------------------------------------
@DmytroVasin
DmytroVasin / db_to_fixtures.rb
Last active April 30, 2020 07:37
Generate Rails test fixtures yaml from database dump
namespace :db do
desc 'Convert development DB to Rails test fixtures'
task to_fixtures: :environment do
TABLES_TO_SKIP = %w[ar_internal_metadata delayed_jobs schema_info schema_migrations].freeze
begin
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.tables.each do |table_name|
next if TABLES_TO_SKIP.include?(table_name)
@DmytroVasin
DmytroVasin / posts_controller.rb
Last active September 25, 2019 09:08
posts_controller.rb
class PostsController < ApplicationController
def index
render cell: :index, model: posts
end
def show
post_view_registrar.commit_view
show_post_page
end
## EASY:
```
def trap(height)
drops = 0
height.each_with_index do |current_height, i|
max_occupancy = [height[0..i].max, height[i..-1].max].min
drops += max_occupancy - current_height
end
module RuboCop
module Cop
module CustomCops
class TimecopReplacerCop < Cop
# ...
def autocorrect(arg_pair)
date_string = eval(arg_pair.value.source).strftime('%Y-%m-%dT%H:%M:%S%z')
new_source = String.new("freezed_time: '#{date_string}'")
->(corrector) do
module RuboCop
module Cop
module CustomCops
class TimecopReplacerCop < Cop
MSG = 'Broken rule'.freeze
def on_send(node)
return unless [:describe, :context, :it, :xit, :it_behaves_like].include?(node.method_name)
return unless node.last_argument&.hash_type? # Syntax: "it { is_expected.to be true }"
rubocop --format simple \
--require /absolute/path/to/rewrite_time_cop.rb \
--only CustomCops/TimecopReplacerCop
# BEFORE:
describe 'some_description', vcr: true, freezed_time: Time.utc(2016,12,9,15,0,0) do
describe 'some_description', freezed_date: Date.new(2017, 9, 4) do
context 'some_description', freezed_time: Time.new(2016, 0o2, 22, 0o0, 11, 44, "+00:00") do
context 'some_description', freezed_date: Time.new(2016, 01, 20, 17, 43, 56, '+03:00') do
context 'some_description', freezed_date: DateTime.new(2017, 3, 17, 7, 40) do
it 'some_description', freezed_date: Time.utc(2016,11,28,15) do
# AFTER:
describe 'some_description', vcr: true, freezed_time: '2016-12-09T15:00:00+0000' do
@DmytroVasin
DmytroVasin / nokogiri_install.txt
Last active November 1, 2018 11:28
Mojave + Nokogiri instalation.
# The main idea to install three libraries.
# These libraries can't be symlinked because they are macOs-provided
# > Refusing to link macOS-provided software: libxml2
# So, You need to specify lib and include directories.
brew install libxml2
brew install libxslt
brew install libiconv
gem install nokogiri -v '1.6.2.1' -- \
@DmytroVasin
DmytroVasin / task_114.rb
Last active August 7, 2018 05:05
Задача 114: Тетрадь в клеточку
# Идея решения:
# Финальное число точек должно составляется из
# 1. Точки в центре ( начало круга ) ( На рисунке => "0" )
# 2. Точек лежащих на осях координат, умноженное на четыре
# - так как 4тыре направляющие ( На рисунке => "_" )
# 3. Точек лежащих на диагональных линиях, умноженное на четыре
# - так как 4тыре направляющие ( На рисунке => "*" )
# 4. Точки лежащие над и под диагональными линиями, умножаем на 8мь