Skip to content

Instantly share code, notes, and snippets.

@davidbella
davidbella / crowd_funding_create.sql
Created October 4, 2013 03:22
SQL: JOINs and SUMs exercise, crowd funding database
CREATE TABLE projects(
id INTEGER,
title TEXT,
category_id INTEGER,
funding_goal INTEGER,
start_date INTEGER,
end_date INTEGER
);
CREATE TABLE categories(
@davidbella
davidbella / anagram.rb
Created October 7, 2013 12:42
Ruby: Anagram detector class - way cleaner than my old one
class Anagram
def initialize(anagram)
@anagram = anagram
end
def match(words)
words.select do |word|
is_anagram?(word)
end
end
@davidbella
davidbella / binary.handshake.rb
Created October 8, 2013 13:24
Ruby: Secret Handshake - arrays, binary numbers, OOP
# # Binary Secret Handshake
# > There are 10 types of people in the world: Those who understand binary, and those who don't.
# You and your fellow flatirons are of those in the "know" when it comes to binary decide to come up with a secret "handshake".
# ```
# 1 = wink
# 10 = double blink
# 100 = close your eyes
@davidbella
davidbella / jukebox_spec.rb
Created October 9, 2013 12:44
Ruby: Creating a test spec with simplecov for a jukebox
require 'simplecov'
SimpleCov.start
require 'json'
require 'rspec'
require_relative '../lib/jukebox'
require_relative '../lib/song'
#use this song data for your tests
songs = [
@davidbella
davidbella / school.rb
Created October 9, 2013 12:49
Ruby: School spec passing
class School
attr_accessor :roster
def initialize(name)
@roster = {}
end
def add_student(name, grade)
@roster[grade] ||= []
@roster[grade] << name
@davidbella
davidbella / triangle.rb
Created October 9, 2013 13:41
Ruby: Triangle classifier
class TriangleError < StandardError
end
class Triangle
def initialize(s1, s2, s3)
@sides = [s1, s2, s3]
if @sides.any? {|side| side <= 0}
raise TriangleError.new, "Cannot create a triangle "
end
@davidbella
davidbella / person.rb
Created October 10, 2013 13:37
Ruby: Dynamic meta-programming to create attr_accessor like methods on the fly
class Person
def initialize(attributes)
attributes.each do |attribute_name, attribute_value|
##### Method one #####
# Works just great, but uses something scary like eval
# self.class.class_eval {attr_accessor attribute_name}
# self.instance_variable_set("@#{attribute_name}", attribute_value)
##### Method two #####
# Manually creates methods for both getter and setter and then
@davidbella
davidbella / ruby.basics.rb
Created October 11, 2013 15:13
Ruby: Basic Ruby Concepts with some tests
# Download this file:
# https://gist.github.com/aviflombaum/28534c5d69edd2439a7d/download
# Run it from your terminal with:
# ruby ruby.basics.rb
# (Just make sure you are in the right directory)
# ======================================
# Ignore All This Code
# ======================================
@davidbella
davidbella / roman_numerals.rb
Created October 11, 2013 19:30
Ruby: Integer to Roman Numeral converter
class Integer
@@conversion = {
1 => ['I', 'V', 'X'],
2 => ['X', 'L', 'C'],
3 => ['C', 'D', 'M'],
4 => ['M', 'M', 'M']
}
def to_roman
numeral = self
@davidbella
davidbella / serialize.rb
Created October 15, 2013 13:14
Ruby: File serialize basics
RSpec.configure do |config|
# Use color in STDOUT
config.color_enabled = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
# Use the specified formatter
config.formatter = :progress # :progress, :html, :textmate
end