Skip to content

Instantly share code, notes, and snippets.

class AdminController < ApplicationController
set_current_tenant_through_filter
before_action :authenticate_user!
before_filter :load_tenant
def load_tenant
begin
tenant = if session[:current_tenant]
@deep-spaced
deep-spaced / game_of_life.rb
Created June 29, 2016 11:43
Run it with `ruby game_of_life.rb`.
class GameOfLife
def initialize(size, iterations)
@square_size = size
@max_iterations = iterations
end
def run
# Set up the initial array.
@board = Array.new(@square_size)
@deep-spaced
deep-spaced / snake_case_multi.ex
Created April 21, 2016 04:19
Slow brute force approach, but with processes.
# Snake Case multi-process!
defmodule SnakeCase do
def multi do
Agent.start_link(fn -> 0 end, name: __MODULE__)
ranges = Enum.chunk(0..round(:math.pow(2, 20)), 262144)
tasks = Enum.map ranges, fn range ->
Task.async(fn -> subrange(range) end)
end
for task <- tasks, do: Task.await(task)
@deep-spaced
deep-spaced / snake_case.ex
Last active April 23, 2016 01:05
A series of solutions to the snake_case problem posed by @jackc's at ACR 2016.
# From the ACR 2016 coding challenge.
defmodule SnakeCase do
@doc """
Brute force, single process.
"""
def single do
range = 0..round(:math.pow(2, 20))
count = Enum.count(range, fn x -> check_one_bits(x) == 10 end)
@deep-spaced
deep-spaced / tree_generator.rb
Created February 11, 2016 03:58
A tree generator for navigation.
module TreeGenerator
def self.generate_tree
pages = all.select('id, name, depth, parent_id, sort_order').order('sort_order ASC')
ordered_pages = ActiveSupport::OrderedHash.new
pages.each do |page|
ordered_pages[page.id] = {page: page, children: generate_children(page.id, pages)} if page.depth == 0
end
ordered_pages
@deep-spaced
deep-spaced / tree_generator_optimized.rb
Created February 11, 2016 03:56
Optimized version of my super-fancy tree generator.
module TreeGeneratorOptimized
def self.generate_tree
pages = all.select('id, name, depth, parent_id, sort_order').order('sort_order ASC')
ordered_pages = ActiveSupport::OrderedHash.new
parent_hash = Hash.new
pages.each do |page|
parent_hash[page.parent_id] = Array.new if parent_hash[page.parent_id].nil?
parent_hash[page.parent_id].push(page) unless page.nil?
end
def odd(n)
n%2 != 0
end
# Here's what we expect:
if a = odd(5) and a == true
puts "a is odd"
end
# => "a is odd"
# &&
puts "test".is_a?(String) && 54+33 # => 87
puts "test".is_a?(String) && true # => true
puts 54+33 && "test".is_a?(String) # => true
puts true && "test".is_a?(Integer) # => false
puts "test".is_a?(Integer) && true # => false
# ||
puts "true" || "test".is_a?(String) # => "true"
puts "true".size == 12 || "test".is_a?(String) # => true
@deep-spaced
deep-spaced / flow_control.rb
Last active December 15, 2015 03:05
Ruby Boolean logic
return Photo.find(params[:photo][:id]) or Photo.new
# Valid parameter: instance from database
# Invalid parameter: new instance
return photo = Photo.find(params[:photo][:id]) and photo.update(comment: params[:photo][:comment]) and photo
# Valid ID and successful update: photo instance
# Valid ID and failed update: result of update
# Invalid ID: nil
# Yes, I work in Rails. You got a problem with that?!?
@deep-spaced
deep-spaced / random.rb
Created December 10, 2015 01:19
Rails: After Initialize example
class Random < ActiveRecord::Base
after_initialize :check_data
after_initialize :save_data
before_save :check_data
before_save :save_data
private
def check_data