Skip to content

Instantly share code, notes, and snippets.

View mkreyman's full-sized avatar
🏃‍♂️
Building cool stuff, serving customers, and enjoying every moment of it...

Mark Kreyman mkreyman

🏃‍♂️
Building cool stuff, serving customers, and enjoying every moment of it...
View GitHub Profile
@mkreyman
mkreyman / pipe_debug.ex
Last active May 14, 2018 19:49
Support the injection of inspect capabilities in an Elixir pipe |> pipestream.
defmodule Util.PipeDebug do
@moduledoc """
Support the injection of inspect capabilities in an elixir pipe |> pipestream.
"""
defmacro __using__(_opts) do
quote do
import Util.PipeDebug, warn: false
end
@mkreyman
mkreyman / gist:265ce1e3dc8448bfec7bd40f88e4832e
Created October 4, 2017 15:07
How to access params and request in routes.rb
...
def with_locale(request)
query = URI.parse(request.original_fullpath).query
return '' unless query
query_params = query.split('?').inject({}) { |h, q| k, v = q.split('='); h[k] = v; h }
locale = query_params['locale']
locale ? "?locale=#{locale}" : ''
end
Rails.application.routes.draw do
module Enumerable
def to_histogram
inject(Hash.new(0)) { |h, x| h[x] += 1; h}
end
end
class Bag
def initialize
@bag_of_objects = []
end
# app/models/user.rb with custom uniqueness validation errors
class User < ActiveRecord::Base
...
def create_or_update
super
rescue ActiveRecord::RecordNotUnique => e
case e.message
when /index_users_on_login/
class AddUniquenessConstraintToUsersGuid < ActiveRecord::Migration
def up
# Delete all but the first user for users with duplicate :guid
duplicate_guids = User.select(:guid).group(:guid).having('count(*) > 1').pluck(:guid)
duplicate_guids.each do |duplicate_guid|
next unless duplicate_guid.present?
users_to_be_deleted = User.where(guid: duplicate_guid).order('id ASC')[1..-1]
users_to_be_deleted.each(&:destroy)
end
# 1. This is a numbered list of twelve statements.
# 2. Exactly 3 of the last 6 statements are true.
# 3. Exactly 2 of the even-numbered statements are true.
# 4. If statement 5 is true, then statements 6 and 7 are both true.
# 5. The 3 preceding statements are all false.
# 6. Exactly 4 of the odd-numbered statements are true.
# 7. Either statement 2 or 3 is true, but not both.
# 8. If statement 7 is true, then 5 and 6 are both true.
# 9. Exactly 3 of the first 6 statements are true.
# 10. The next two statements are both true.
module Enumerable
def to_histogram
inject(Hash.new(0)) { |h, x| h[x] += 1; h}
end
end
class Bag
def initialize
@bag_of_objects = []
end
# spec/spec_helper.rb
def permutations(num)
[true, false].repeated_permutation(num)
end
# spec/models/welcome_email_logic_spec.rb
require 'spec_helper'
describe WelcomeEmail do
module Enumerable
def to_histogram
inject(Hash.new(0)) { |h, x| h[x] += 1; h}
end
end
p [1, 2, 2, 2, 3, 3].to_histogram
# => {1=>1, 2=>3, 3=>2}
p ["a", "b", nil, "c", "b", nil, "a"].to_histogram
@mkreyman
mkreyman / ruby_benchmark_examples.rb
Created August 15, 2017 16:41 — forked from SabretWoW/ruby_benchmark_examples.rb
Three examples showing three of the main methods of Ruby's Benchmark class that's used to profile your code. Benchmark.measure at the most basic, Benchmark.benchmark allows you to run trials of different code blocks, and Benchmark.bmbm that helps you establish a baseline for your tests.
# http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html
require 'benchmark'
# http://www.ruby-doc.org/stdlib-2.0.0/libdoc/bigdecimal/rdoc/BigMath.html
require 'bigdecimal/math'
# Set the number of iterations to run. The underscore here is used as a substitute for normal comma so Ruby interprets the number correctly.
iterations = 10
puts "\nCalculating pi #{iterations} times.\n\n"