Skip to content

Instantly share code, notes, and snippets.

@rsslldnphy
rsslldnphy / optional_records.rb
Created May 20, 2013 10:03
Options used for dealing with missing records
## Model code
class Customer < ActiveRecord::Base
def find_by_name(name)
Option[where(name: name).first]
end
end
## Controller code
"Nils - what are your Options?" - Russell Dunphy
Tony Hoare, inventor of the null reference, calls it his "billion dollar mistake".
Although Ruby goes some way to correcting it by making `nil` at least be a class
with some reasonably helpful stuff on it, you'll no doubt have been hit by a
`NoMethodError: undefined method 'foo'` just as many times as me.
So, given this problem exists, I want to convince you of a few things.
First, that there are not one, but two types of `nil`.
@rsslldnphy
rsslldnphy / sml-tdd-loop.el
Last active December 11, 2015 08:18
Emacs function to help create a TDD loop when writing SML
(defun restart-sml ()
(sml-prog-proc-switch-to)
(end-of-buffer)
(comint-delchar-or-maybe-eof 0))
(defun run-sml-tests-now ()
(let* ((code-file-name
(replace-regexp-in-string "_tests" "" (buffer-file-name)))
(test-file-name
(replace-regexp-in-string "\.sml" "_tests.sml" code-file-name)))
@rsslldnphy
rsslldnphy / peg_spec.rb
Created December 4, 2012 19:56
Some ways of piping and composing methods
require 'rspec'
class Object
def peg(&block)
block.call(self)
end
def wend(arg)
Wender.new(self, arg)