Skip to content

Instantly share code, notes, and snippets.

@ericroberts
ericroberts / calculator.rb
Last active August 29, 2015 14:18
Intro to TDD
class Calculator
attr_reader :string_to_calculate
def initialize(string_to_calculate, options = {})
@string_to_calculate = string_to_calculate
@delimiter = options[:delimiter]
end
def add
raise ArgumentError, error_message unless negatives.empty?
@ericroberts
ericroberts / anagram.js
Last active August 29, 2015 14:17
Doing the anagram exercise from exercism.io
var anagram = function(word) {
return new Anagram(word);
};
function Anagram(word) {
this.matches = matches;
function matches() {
possible_anagrams = retrieveAnagrams(arguments);
return possible_anagrams.filter(function(anagram) {
@ericroberts
ericroberts / sti_explanation.md
Last active April 7, 2020 13:49
STI where inheritance column values are not the same as the name of the class

I recently tried to retrofit STI on a database table that had already existed for a while. Here's a basic outline of the scenario.

  1. I had a class 'Code' and a database table 'codes'.
  2. 'Code' had an attribute 'units', which could be either '$' or '%'
  3. I wanted the STI classes to be Code::Dollar or Code::Percent

I successfully implemented this with the following:

class Code
@ericroberts
ericroberts / _description.md
Last active August 29, 2015 14:02
Duplication Removal

Which version of the code do you find more clear and/or preferable?

@ericroberts
ericroberts / programming-without-if.md
Last active August 29, 2015 14:01
Conditionals are Code Smells: Programming Without "If"

Conditionals are Code Smells: Programming Without "If"

If/then/else is one of the first things a new programmer learns. But, like any tool, it can be overused. Too much conditional logic makes your methods hard to understand and reason about. What if we started thinking about our code differently? What if we started telling our code what to do instead of asking it what it can do? This talk will go through a real refactoring from a method with more than 30 if/then/else statements to a solution with no conditional logic whatsoever.