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 AdminController < ApplicationController | |
set_current_tenant_through_filter | |
before_action :authenticate_user! | |
before_filter :load_tenant | |
def load_tenant | |
begin | |
tenant = if session[:current_tenant] |
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 GameOfLife | |
def initialize(size, iterations) | |
@square_size = size | |
@max_iterations = iterations | |
end | |
def run | |
# Set up the initial array. | |
@board = Array.new(@square_size) |
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
# 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) |
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
# 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) |
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 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 |
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 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 |
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 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" |
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
# && | |
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 |
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
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?!? |
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 Random < ActiveRecord::Base | |
after_initialize :check_data | |
after_initialize :save_data | |
before_save :check_data | |
before_save :save_data | |
private | |
def check_data |
NewerOlder