Skip to content

Instantly share code, notes, and snippets.

@twohlix
twohlix / hash_key_test.rb
Created June 24, 2016 18:30
Ruby Hash key performance checking
require 'benchmark'
string_keys = []
symbol_keys = []
int_keys = []
keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
count = 0
keys.each do |i|
def PArentController < ActiveRecordControllerthingy
before_action :set_jobs
def show
end
private
def set_jobs
@jobs = Job.all # or whatever
@twohlix
twohlix / onehundredmillion.eightthreads
Last active August 29, 2015 14:22
Chutes and Ladders Output
$ time ruby chutes_and_ladders.rb -g 100000000 -t 8
Simulating 100000000 games of chutes and ladders on 8 threads
0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 12878
@twohlix
twohlix / locally_cacheable.rb
Last active August 29, 2015 14:20
Locally Cacheable non ActiveRecord model
module LocallyCacheable
extend ActiveSupport::Concern
included do
end
module ClassMethods
def local(id)
attrs = local_attributes id
return new(attrs) if attrs.present?
@twohlix
twohlix / SomeOutput.sh-session
Last active August 29, 2015 14:09
Any? vs Each
twohlix$ ruby any_vs_each.rb 10000
user system total real
100%
each: 0.000000 0.000000 0.000000 ( 0.000544)
any: 0.000000 0.000000 0.000000 ( 0.000652)
90%
each: 0.000000 0.000000 0.000000 ( 0.000475)
any: 0.000000 0.000000 0.000000 ( 0.000844)
@twohlix
twohlix / example.rb
Created October 2, 2014 21:38
Add Errors Once
message = 'This is some error message'
errors.add(:attribute, message) unless errors.added?(:attribute, message)
# or should we create an add_once on errors. Can be done as a monkey patch but a core contribution would be better
def ActiveRecord::Errors
def add_once(attribute, message)
add(attribute, message) unless added?(attribute, message)
end
end
@twohlix
twohlix / MONO.sublime-build.json
Last active December 18, 2015 03:38
Sublime Text 2 build system for Mono
{
"cmd": ["gmcs", "$file"],
"file_regex": "(^[\\w\\d\\s:\\\\.]+)\\(([0-9]+),([0-9]+)",
"windows":{
"shell": "cmd.exe"
}
}
@twohlix
twohlix / door_unlock.php
Created February 23, 2012 04:56
Apartment Door Unlocker - twilio style
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
function randomResponseString(){
$responses = array( 'Sup wid it?', 'Helloooooooooooooo',
'Noob Noob Noob Noob', 'Let the Base Cannon Kick It: wub wub wub wub',
'A horse passes behind you', 'Help I am stuck in an apartment buzzer factory. Please send help!',
'Skynet became self aware at '.date('g h A').' today.', 'Access Denied',
'I will grant you access if you spin around 3 times! Go on. Do it. Hooray',
@twohlix
twohlix / blog.module
Created August 31, 2011 20:07
Get Nids from Views Results into another View passively
//////////////////////////////////////////////////////////////////////
// Get Nids from Views Results into another View passively
//
// Problem with below code is caching
// 'featured_nids' may not get updated before views_pre_view
// this will lead to an issue where the args set on 'blogs'
// will always be lagging behind by 1 cache clear.
//hook_views_pre_view
function blog_views_pre_view(&$view, &$display_id, &$args){
@twohlix
twohlix / blog.module
Created August 31, 2011 20:05
Get Nids from Views Results into another View passively
//////////////////////////////////////////////////////////////////////
// Get Nids from Views Results into another View passively
//
// Problem with below code is caching
// 'featured_nids' may not get updated before views_pre_view
// this will lead to an issue where the args set on 'blogs'
// will always be lagging behind by 1 cache clear.
//hook_views_pre_view
function blog_views_pre_view(&$view, &$display_id, &$args){