Skip to content

Instantly share code, notes, and snippets.

View michaelfairley's full-sized avatar

Michael Fairley michaelfairley

View GitHub Profile
#!/usr/bin/env python
import mincemeat
data = ["Humpty Dumpty sat on a wall",
"Humpty Dumpty had a great fall",
"All the King's horses and all the King's men",
"Couldn't put Humpty together again",
]
def mapfn(k, v):
@michaelfairley
michaelfairley / gist:1028707
Created June 16, 2011 05:12
or vs. || and and vs. &&
a = nil || 1
puts a # prints 1
b = nil or 1
puts b # prints nil
c = 1 && 2
puts c # prints 2
ruby-1.8.7-p330 :001 > 1 + 1
=> 2
ruby-1.8.7-p330 :002 > a = _
=> 2
ruby-1.8.7-p330 :003 > a
=> 2
@michaelfairley
michaelfairley / gist:1066155
Created July 5, 2011 23:00
Object#methods
> "@RubyTips".methods.sort
=> [:!, :!=, :!~, :%, :*, :+, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :[], :[]=, :__id__, :__send__, :ascii_only?, :between?, :bytes, :bytesize, :capitalize, :capitalize!, :casecmp, :center, :chars, :chomp, :chomp!, :chop, :chop!, :chr, :class, :clear, :clone, :codepoints, :concat, :count, :crypt, :define_singleton_method, :delete, :delete!, :display, :downcase, :downcase!, :dump, :dup, :each_byte, :each_char, :each_codepoint, :each_line, :empty?, :encode, :encode!, :encoding, :end_with?, :enum_for, :eql?, :equal?, :extend, :force_encoding, :freeze, :frozen?, :getbyte, :gsub, :gsub!, :hash, :hex, :include?, :index, :initialize_clone, :initialize_dup, :insert, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :intern, :is_a?, :kind_of?, :length, :lines, :ljust, :lstrip, :lstrip!, :match, :method, :methods, :next, :next!, :nil?, :object_id, :oct, :ord, :partition, :private_m
@michaelfairley
michaelfairley / gist:1240348
Created September 25, 2011 07:36
Draper benchmark
require 'benchmark'
require 'draper'
class Model
# 300 is about the fewest methods an ActiveRecord model will have
300.times do |i|
define_method :"method_#{i}" do
i
end
end
@michaelfairley
michaelfairley / Gemfile
Created July 26, 2012 17:39
ActiveRecord cannot be eager loaded without ActionDispatch
source :rubygems
gem 'activerecord', :require => 'active_record'
def memoize(&blk)
cache = {}
lambda do |*args|
return cache[args] if cache.has_key?(args)
cache[args] = blk.call(*args)
end
end
define_singleton_method(:fib, &memoize{ |n|
if n < 2
@michaelfairley
michaelfairley / open_questions
Created October 16, 2012 04:36
Ruby version of Jersey
the `path_param`/`auth` stuff seems ugly :-/
How to convert response to json, etc. (ActiveModel Serializers?)
Providers?
Routing/registry
@michaelfairley
michaelfairley / immutable-ruby.md
Created October 31, 2012 03:12
Immutable Ruby (Brit Ruby)

Immutable Ruby

Most Ruby code makes heavy use of mutable state, which often contributes to long term maintenance problems. Mutability can lead to code that's difficult to understand, libraries and applications that aren't thread-safe, and tests that are slow and brittle. Immutability, on the other hand, can make code easy to reason about, safe to use in multi-threaded environments, and simple to test. Doesn't that sound nice?

This talk answers the question "why immutability?", covers the building blocks of immutable code, such as value objects and persistent data structures, as well as higher order concepts that make heavy use of immutability, such as event sourcing and pure functions, and finally discusses the tradeoffs involved in going immutable.

Michael Fairley

Michael Fairley is a developer at Braintree Payments, where he uses Ruby to make it easy for businesses around the world (including the UK!) to accept credit card payments online. He’s an active open source contributor, and maintains a han

@michaelfairley
michaelfairley / README.md
Created November 24, 2012 18:01
jruby-go

jruby-go

Go's concurrency model on top of JRuby

Channels

Channel.new creates a channel, and you can optionally pass it a capacity: Channel.new(200)

Use << to send on a channel: chan << "stuff"