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 / gist:1891aaa70e41a3519a7487e7742a5eda
Last active February 15, 2018 19:01
Spiral traversal of a multidimensional list of characters
# Given a multidimensional list of characters, print out a spiral traversal of the lists.
#
# iex(1)> matrix = [~w(A B C D), ~w(E F G H), ~w(I J K L), ~w(M N O P)]
# [
# ["A", "B", "C", "D"],
# ["E", "F", "G", "H"],
# ["I", "J", "K", "L"],
# ["M", "N", "O", "P"]
# ]
# iex(2)> Spiral.traverse(matrix, [])
@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