Skip to content

Instantly share code, notes, and snippets.

View knzai's full-sized avatar

Kenzi Connor knzai

View GitHub Profile
@knzai
knzai / self_mutation.rb
Created February 12, 2015 23:44
returning self means mutation
# If you return self you have either mutated self, mutated something else (essentially global, even worse)
# or thrown away the change as a NOOP. Let's play with examples?

Keybase proof

I hereby claim:

  • I am timocratic on github.
  • I am timocratic (https://keybase.io/timocratic) on keybase.
  • I have a public key whose fingerprint is 7E07 1867 9F3F 2174 FA58 27C5 B405 8899 5CD3 22B9

To claim this, I am signing this object:

@knzai
knzai / uri_validator.rb
Last active December 14, 2015 16:09 — forked from bluemont/url_validator.rb
Totally untested, just forked and tweaked for discussion
require 'addressable/uri'
#Accepts options[:message] and options[:allowed_protocols]
class UriValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
uri = parse_uri(value)
if !uri
record.errors[attribute] << generic_failure_message
elsif !allowed_protocols.include?(uri.scheme)
@knzai
knzai / lsof.bash
Created October 9, 2012 17:11
lsof command to find all things listening on ports
lsof -Pnl +M -i4 | grep LISTEN
@knzai
knzai / bootstrap.sh
Created October 4, 2011 07:05
An example bash bootstrap to get workstation set-up with chef - update YOUR_ORG below
#!/usr/bin/env sh
#After forking and editing YOUR_ORG to point at the right repo can be run via:
#bash <(curl -s https://raw.github.com/gist/THEIDOFYOURFORKEDGIST)
#if you use `curl xxx | sh` input will be messed up and there will be no pauses
#I keep XCode and gcc on a thumbdrive along with java, for speeding things up
#http://support.apple.com/downloads/DL1421/en_US/JavaForMacOSX10.7.dmg
read -p "Install XCode or gcc (http://github.com/kennethreitz/osx-gcc-installer)"
#setup your ssh keys for github
@knzai
knzai / composition_over_inheritance_in_mongo_mapper.rb
Created March 20, 2011 20:19
A pseudo-OODB style of using MongoMapper's plugin system to create composed objects.
#rvm use 1.9.2
#brew install mongo
#gem install mongo_mapper
#gem install bson_ext
require 'rubygems'
require 'mongo_mapper'
MongoMapper.database = 'mm_demo'
@knzai
knzai / 1_intro_to_subject.rb
Created February 8, 2011 07:45
A pattern for testing class methods in ruby with rspec explicit subjects
# RSpec's subject method, both implicitly and explicitly set, is useful for
# declaratively setting up the context of the object under test. If you provide a
# class for your describe block, subject will implicitly be set to a new instance
# of this class (with no arguments passed to the constructor). If you want
# something more complex done, such as setting arguments, you can use the
# explicit subject setter, which takes a block.
describe Person do
context "born 19 years ago" do
subject { Person.new(:birthdate => 19.years.ago }
it { should be_eligible_to_vote }
@knzai
knzai / one_hundred_percent.html
Created October 9, 2010 06:02
A standards based layout using table styles - with an IE quirks mode fallback
<!-- keep IE in quirks mode -->
<!DOCTYPE html>
<html>
  <head>
<!-- This following meta tag does no good since there is no way to trigger quirks
mode only in IE7. The problems with an xml prolog that triggered quirks mode were
fixed in IE7, but this meta tag was not yet introduced, so the only thing you can do
is trigger IE-wide quirks via the comment before the DOCTYPE //-->
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    <style type="text/css">
def self.logger
@logger ||= begin
(defined?(Rails) && Rails.logger) ||
(defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER) ||
default_logger
end
end
#In case it wasn't obvious, this was a joke about Rails anti-patterns
def address(separator, seperator2=' ')
fields_array = %w(address1 address2 city state zip phone).map {|t| [:billing,t].join('_').to_sym}
(0..10).inject("") { |string, i|
if i.odd?
field = :separator
field = (field.to_s + '2').intern if i == 7
else
field = fields_array[i/2]
end