This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |