Skip to content

Instantly share code, notes, and snippets.

View mikecmpbll's full-sized avatar
🎯
Focusing

Mike Campbell mikecmpbll

🎯
Focusing
  • Skipton, North Yorkshire
View GitHub Profile
# /etc/init/sidekiq.conf - Sidekiq config
# This example config should work with Ubuntu 12.04+. It
# allows you to manage multiple Sidekiq instances with
# Upstart, Ubuntu's native service management tool.
#
# See workers.conf for how to manage all Sidekiq instances at once.
#
# Save this config as /etc/init/sidekiq.conf then mange sidekiq with:
# sudo start sidekiq index=0
# sudo stop sidekiq index=0
print "Enter an integer number to be calculated as factorial: "
Integer(gets) rescue print "Hey, that's not an integer! Enter an integer, please: "
n = gets.to_i
def factorial n
n > 1 ? n * factorial(n - 1) : 1
end
puts "Factorial of number #{n} is #{factorial(n)}"
@mikecmpbll
mikecmpbll / str_in_lines.rb
Created April 21, 2015 16:15
Split a ruby string into X roughly even length lines, but don't break words.
def str_in_lines(str, x)
word_break_indices = []
str.to_enum(:scan,/\b/).map do |m,|
word_break_indices << $`.size
end
perfect_split_indices = (0..str.length).step(str.length/x).to_a
actual_split_indices = perfect_split_indices.map{ |si| word_break_indices.min_by{ |wbi| (wbi - si).abs } }
actual_split_indices.push(str.length) unless actual_split_indices.last == str.length
@mikecmpbll
mikecmpbll / flow.rb
Created April 15, 2015 13:03
hack around adding invalid objects to a has_many association
u = User.new
u.valid? == false
u.save(validate: false)
params = { dont_validate_user_ids: true, user_ids: [1] }
UserGroup.create(params)
User.first
=> #<User id: 1, name: nil, user_group_id: 1, created_at: "2015-04-15 12:37:58", updated_at: "2015-04-15 12:37:58">
def act(&block)
timeout = Time.now + 10
work = Thread.new { block.call }
while Time.now < timeout && work.alive? do
sleep(0.1)
end
if work.alive?
puts "hit timeout"
irb(main):006:0> Benchmark.measure { 100_000.times { [1, 2, 2, 3, 3, 3].each_with_object(Hash.new{ |x,k| x[k] = 0 }){ |x,s| s[x] += 1 }.max_by{ |k,v| v }[0] } }
=> #<Benchmark::Tms:0x007fc3e180b108 @label="", @real=0.899188, @cstime=0.0, @cutime=0.0, @stime=0.010000000000000009, @utime=0.8799999999999999, @total=0.8899999999999999>
irb(main):007:0> Benchmark.measure { 100_000.times { [1, 2, 2, 3, 3, 3].group_by{|e| e}.values.max_by(&:size).first } }
=> #<Benchmark::Tms:0x007fc3dffbe7d0 @label="", @real=0.523452, @cstime=0.0, @cutime=0.0, @stime=0.010000000000000009, @utime=0.5099999999999998, @total=0.5199999999999998>
@mikecmpbll
mikecmpbll / code
Last active August 29, 2015 14:18 — forked from baditaflorin/code
#!/usr/bin/env ruby
# encoding: utf-8
require 'savon'
require 'csv'
startDate = Date.new(2004, 01, 01)
stopDate = Date.today
csvHeaders = ['numar', 'numar_vechi', 'data', 'institutie', 'departament',
'categorie_caz', 'stadiu_procesual', 'obiect', 'data_modificare','nume','calitate_parte','solutie']
@mikecmpbll
mikecmpbll / predictions.log
Created March 4, 2015 18:24
Chepstow 7/3/15
================================================================================
Chepstow (soft) - 13:50 (stake: [0.02, 1, 0.4981700000000018])
Horse Adj Unadj %ile Notes
-------------------------------------------------------------------------
Cruising Bye 70.60318 51.74106 58.47849 (24.8f), $0$
Always Bold 71.10135 49.85184 76.03137 (24.82f), $0$
Donapollo 73.63729 52.82733 42.64654 (23.35f), $0$
Wayward Frolic 73.91475 52.64498 41.71131 (22.04f), $0$
Cyrien Star 74.9877 49.1907 79.01786 (25.5f), $0$
Paddy The Oscar 75.87846 51.70213 60.39574 (24.67f), $0$
@mikecmpbll
mikecmpbll / versioning.rake
Last active August 19, 2016 21:08
A wee set of rake tasks to help bump version number using git tags.
LEVELS = [:major, :minor, :patch]
def version
@version ||= begin
v = `git describe --always --tags`
{}.tap do |h|
h[:major], h[:minor], h[:patch], h[:rev], h[:rev_hash] = v[1..-1].split(/[.-]/)
end
end
end
class ApplicationController < ActionController::Base
end
class SharedController < ApplicationController
before_action :foo
before_action :bar
def foo
#stub
end