Skip to content

Instantly share code, notes, and snippets.

require 'singleton'
class AtMost
include Singleton
def initialize
@table = Hash.new { |h, k| h[k] = -1 }
end
def call(n)
class Container {
constructor() {
this.defResolvers = {};
this.defPromises = {};
}
require(depList, body) {
let depPromises = depList.map(d => this.defPromise(d));
return Promise.all(depPromises)
.then((deps) => body(...deps))
[17] pry(main)> puts Post.all.select(1).to_sql
SELECT 1 FROM "posts"
=> nil
[18] pry(main)> puts Post.all.select(1).limit(1).to_sql
SELECT  1 FROM "posts" LIMIT 1

Why put two space after SELECT?

def build(str)
str = str.dup
str.prepend '\A'
str.gsub! %r{/:([a-z]+)}, '/(?<\1>[^\/]+)'
str << '(?<format>\.[a-z]\w*)?'
str << '\z'
Regexp.new(str)
end
rx = build('/posts/:id/comments')
module ToBoolean
def self.cast(value)
@type ||= ActiveRecord::Type::Boolean.new
!! @type.type_cast_from_user(value)
end
end
class Yo < ActiveRecord::Base
def self.ordered_find(ids)
where(id: ids).to_a.index_by(&:id).values_at(*ids)
end
end

Keybase proof

I hereby claim:

  • I am marshall-lee on github.
  • I am async_prince (https://keybase.io/async_prince) on keybase.
  • I have a public key ASDBDye6iuyGh7j0xupyz5-4kpssL5tbVWR0aeyyl4k7bQo

To claim this, I am signing this object:

# app/models/document.rb
class Person < ActiveRecord::Base
validate &PassportValidator
end
# lib/yoba_validator.rb
class YobaValidator < SimpleDelegator
include ActiveModel::Validations
# Prevent HTTP requests to be made into private network (in Ruby!)
# Just an example, don't use it in production!
require 'http'
require 'resolv'
class StaticResolveTCPSocket
def initialize(lookup)
@lookup = lookup.dup
@lookup.default_proc = proc do |_h, host|
# decompress gzip-ed VCR fixtures
require 'zlib'
require 'psych'
Dir.glob('spec/fixtures/vcr_cassettes/**/*.yml').each do |filename|
data = Psych.load_file(filename)
changed = false
data['http_interactions'].each do |interaction|