Skip to content

Instantly share code, notes, and snippets.

@litch
litch / gist:6543765
Last active December 22, 2015 22:59
Storing Regex in a constant instead of a method saves a lot of memory, but not a lot of time.
?> def get_memory_usage
>> `ps -o rss= -p #{Process.pid}`.to_i
>> end
=> nil
>>
?> before = get_memory_usage
=> 20296
>>
?> n.times {MethodRegexObject.new}
@litch
litch / percentile_finder.rb
Created August 8, 2013 18:01
MongoDB Logger Request timing percentile finder
class PercentileFinder
attr_accessor :action, :controller, :limit, :calls, :dataset
def initialize(action, controller, limit = 1000)
self.action = action
self.controller = controller
self.limit = limit
end
def find(percentile_looking_for = 0.95)
@litch
litch / rpn_3_ways.rb
Last active December 20, 2015 01:29
RPN 3 ways. I had two - then tried to do the pattern-matching solution, wound up being intermediate performance...
require 'rspec/autorun'
require 'benchmark'
describe "polish notation" do
it "do a trivial case" do
polish_parser(:+, 1, 1).first.should == 2
reverse_stack_polish_parser(:+, 1, 1).first.should == 2
pm_polish_parser(:+, 1, 1).first.should == 2
end
@litch
litch / merchant_account_test.rb
Created May 28, 2013 19:44
crazy epic unit test
require 'test_helper'
class MerchantAccountTest < ActiveSupport::TestCase
setup do
@merchant_account = FactoryGirl.create(:merchant_account)
end
should "fetch authorized orders" do
@order = FactoryGirl.create(:order, venue: @merchant_account.venue)
credit_card_hash = {
class SpeakersController < ApplicationController
load_and_authorize_resource :event
load_and_authorize_resource :speaker, through: [:event], shallow: true #the array here could only be the single symbol but since i almost always wind up adding more, i just start with it
before_filter :new_speaker, :only => [:new, :create]
respond_to :html
def index
end
def show
@litch
litch / Gemfile
Created February 24, 2013 17:34
Ruby 2.0.0 installation with RVM and running on heroku. RVM instructions sourced from: https://coderwall.com/p/tptocq A quick comparison of Ruby 2.0.0 performance loading rails can be found at my blog: www.superpumpup.com
source 'https://rubygems.org'
ruby "2.0.0"
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
...
App.Router.map(function() {
this.resource('tables', function() {
this.resource('table', {path: ':table_id'});
});
});
@litch
litch / can_be_in_diff_places.js
Created January 28, 2013 22:07
javascript to have a button do something but still pass through
$("#my_button_id").click(function () {
call_my_other_js;
});)
$("#my_button_id").click(function () {
$("form#formID").submit();
});)
$("#my_button_id").click(function () {
do_thing_3_for_kicks;
});)
require "xpath" # XPath is a separate gem now
module Cucumber
module Rails
module CapybaraSelectDatesAndTimes
def select_date (date, options = {})
date = Date.parse(date)
# lookup id prefix by label
id_prefix = id_prefix_for(options[:label])
@litch
litch / equipment_item.rb
Created January 23, 2013 17:58
Refactor me challenge!
class EquipmentItem
attr_accessor :task_items, equipment_hauls
def current_location
last_task = task_items.sort_by{|a| a.end_time }.last
last_haul = equipment_hauls.last
if last_task && last_haul
if last_task.end_time > last_haul.performed_at
last_task.timesheet.job
else