Skip to content

Instantly share code, notes, and snippets.

View kmayer's full-sized avatar

Ken Mayer kmayer

View GitHub Profile
@kmayer
kmayer / fizzbuzz.rb
Last active March 13, 2017 03:55
FizzBuzz to 100 in one line of (very compact) ruby. And other variants
# How to use and experiment
# $ irb -I. -rfizzbuzz
# => fb = FizzBuzz.new
# => fb.enumerator_with_array_indices.take(30) == FizzBuzz.EXPECTATIONS
# => true
class FizzBuzz
EXPECTATIONS=[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26, "Fizz", 28, 29, "FizzBuzz"]
def shortest # 100 chars of ruby
@kmayer
kmayer / sidekiq.rb
Created September 8, 2013 17:41
Sidekiq Exception tracer for NewRelic Add this to your sidekiq initializer
module NewRelic
class SidekiqException
def call(worker, msg, queue)
begin
yield
rescue => exception
NewRelic::Agent.notice_error(exception, :custom_params => msg)
raise exception
end
end
@kmayer
kmayer / player.rb
Created August 27, 2013 20:37
rubywarrior (Level 6) with Ryan Spore
Command = Struct.new(:method, :direction)
class Command
def initialize(m, *args)
@method = m
@direction = args
end
def perform(warrior)
warrior.send(@method, *@direction)
@kmayer
kmayer / heuristics.rb
Created August 25, 2013 18:52
RubyWarrior beginner Total Score: 525 + 101 = 626 Your average grade for this tower is: S Level 1: S Level 2: A Level 3: S Level 4: S Level 5: A Level 6: S Level 7: S Level 8: S Level 9: S
module Heuristics
TARGET_PRIORITY = {
RubyWarrior::Units::Archer => 1,
RubyWarrior::Units::Wizard => 2,
RubyWarrior::Units::ThickSludge => 3,
RubyWarrior::Units::Sludge => 4,
RubyWarrior::Units::Captive => 999,
RubyWarrior::Units::Warrior => 999,
}
@kmayer
kmayer / SpecRunner.html
Last active December 16, 2015 08:59
Sample Jasmine spec runner for Sencha Touch 2.1
<!DOCTYPE html>
<html>
<head>
<title>Jasmine Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="jasmine-1.3.1/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="jasmine-1.3.1/jasmine.css">
<script type="text/javascript" src="jasmine-1.3.1/jasmine.js"></script>
<script type="text/javascript" src="jasmine-1.3.1/jasmine-html.js"></script>
@kmayer
kmayer / object_creation_methods.diff
Last active December 14, 2015 02:59
Replace dynamic method receptor with dynamic method definition
private
- def method_missing(method, *args)
- if method.to_s =~ /^create_(.+)$/
- send("new_#{$1}", *args).tap(&:save!)
- else
- super
- end
+ def self.included(base)
+ ObjectCreationMethods.public_instance_methods(false).each do |new_method_name|
@kmayer
kmayer / object_creation_methods.diff
Last active December 14, 2015 02:59
Replace create methods with method missing
- def create_user(overrides = {})
- new_user(overrides).tap(&:save!)
- end
-
private
+ def method_missing(method, *args)
+ if method.to_s =~ /^create_(.+)$/
+ send("new_#{$1}", *args).tap(&:save!)
@kmayer
kmayer / object_creation_methods.rb
Last active December 14, 2015 02:59
Object Parental Unit
module ObjectCreationMethods
def new_user(attributes = {})
defaults = {
email: "user#{counter}@example.com",
password: "password",
}
User.new { |user| apply(user, defaults, overrides) }
end
def create_user(attributes = {})
@kmayer
kmayer / gist:3869451
Created October 11, 2012 00:43
Heroku_San AutoTagger sample
# Extensions to heroku_san tasks
# Never runs on Heroku
if Rails.env.development?
STAGES = %w[ci acceptance staging production]
require 'auto_tagger'
def create_and_push(stage)
auto_tag = AutoTagger::Base.new(stages: STAGES, stage: stage, verbose: true, push_refs: false, refs_to_keep: 100)
@kmayer
kmayer / spec-support-selenium.rb
Created April 9, 2012 22:28
Slow down selenium
#Slows down selenium so you can see what's going on
#http://stackoverflow.com/questions/4815516/how-can-i-run-selenium-used-through-capybara-at-a-lower-speed/5150855#5150855
#require 'selenium-webdriver'
#module ::Selenium::WebDriver::Remote
# class Bridge
# def execute(*args)
# result = raw_execute(*args)['value']
# sleep 0.1
# result
# end