Skip to content

Instantly share code, notes, and snippets.

@EdwardDiehl
EdwardDiehl / slow_query_logger.rb
Created Mar 30, 2018
Ruby on Rails – your own slow query log, no sql configuration required
View slow_query_logger.rb
# http://pdabrowski.com/blog/ruby-on-rails/slow-query-log/
# config/initializer/slow_query_logger.rb
class SlowQueryLogger
MAX_DURATION = 3.0
def self.initialize!
ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
duration = finish.to_f - start.to_f
@EdwardDiehl
EdwardDiehl / ruby-blocks-procs-lambdas.md
Created Feb 22, 2018 — forked from cflee/ruby-blocks-procs-lambdas.md
Discoveries about Ruby Blocks, Procs and Lambdas
View ruby-blocks-procs-lambdas.md

Ruby: Blocks, Procs, Lambdas

Note that for blocks, {} and do ... end are interchangeable. For brevity, only the former will be listed here.

Version differences

Pre-1.9, lambda and proc are synonyms. Essentially the difference is between proc and block.

def method(&block) p block.class; p block.inspect; end
l = lambda { 5 }
@EdwardDiehl
EdwardDiehl / ruby_version.rb
Last active Feb 26, 2022
Read and specify ruby version in Gemfile from asdf-vm .tool-versions file.
View ruby_version.rb
# https://gist.github.com/EdwardDiehl/9c660b59d0fb2e936774ee2c4fcfc792
# Read and specify ruby version in Gemfile from asdf-vm .tool-versions file.
# Copy the file to your your project root.
# Add these lines at the top of your project Gemfile:
# require './ruby_version.rb'
# ruby ruby_version
def ruby_version(ruby_default_version = '2.5.0')
tool_versions_file = File.join(File.expand_path('..', __FILE__), '.tool-versions')
@EdwardDiehl
EdwardDiehl / poly_to_osm.rb
Created Feb 3, 2018 — forked from JamesChevalier/poly_to_osm.rb
This is my script to generate OSMs out of all of a country's POLY files.
View poly_to_osm.rb
#!/usr/bin/env ruby
Dir.glob('poly/*.poly').each_slice(50) do |group|
bp_wx = ''
group.each do |poly_file|
file = File.basename(poly_file, '.poly')
city, region = file.split('_')
bp_wx << "--buffer bufferCapacity=50000"\
" --bp file='poly/#{city}_#{region}.poly'"\
@EdwardDiehl
EdwardDiehl / 1-sleep-es7.js
Created Dec 21, 2017 — forked from danharper/1-sleep-es7.js
ES7's async/await syntax.
View 1-sleep-es7.js
// ES7, async/await
function sleep(ms = 0) {
return new Promise(r => setTimeout(r, ms));
}
(async () => {
console.log('a');
await sleep(1000);
console.log('b');
})()
View mocha_vs_jasmine.md

Почему Mocha, а не Jasmine?

Ниже будут приведены аргументы в пользу выбора Mocha

  • Высокая популярность:
    — 4m против 400k загузок в месяц

  • Высокая активность:
    — 1 890 против 1400 коммитов (всего)
    — 171 против 101 коммитов (последний год)

@EdwardDiehl
EdwardDiehl / 000_postgresql_fancy_datatypes
Created Jan 15, 2017 — forked from pcreux/000_postgresql_fancy_datatypes
Postgresql fancy datatypes with Rails / ActiveRecord. Run it with `rake`!
View 000_postgresql_fancy_datatypes
# Postgresql fancy datatypes!
* array
* hstore (=~ hash)
* json
* jsonb
Philippe Creux - [@pcreux](http://twitter.com/pcreux)
@EdwardDiehl
EdwardDiehl / reseed.rake
Created Jan 7, 2017 — forked from nithinbekal/reseed.rake
Rake task to reset and seed Rails database
View reseed.rake
# Originally written by Justin French (2008):
# http://justinfrench.com/notebook/a-custom-rake-task-to-reset-and-seed-your-database
#
# Modified to work with Rails 4.
desc 'Raise an error unless development environment'
task :safety_check do
raise "You can only use this in dev!" unless Rails.env.development?
end
@EdwardDiehl
EdwardDiehl / description.markdown
Created Jan 6, 2017 — forked from runemadsen/description.markdown
Reverse polymorphic associations in Rails
View description.markdown

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

@EdwardDiehl
EdwardDiehl / README.md
Created Jun 11, 2016 — forked from iamatypeofwalrus/README.md
A Rails 4 pluck in batches implementation
View README.md

pluck_in_batches

Sometimes you need to iterate over a ton of items and you don't want the overhead of creating AR objects out of all of them. Hell, you only need a few things! Well, #pluck has your back.

But what if you want to iterate over many tonnes of items?

Pluck in batches to the rescue!

This isn't the exact code that I use in my code base, but it is damn close.