Skip to content

Instantly share code, notes, and snippets.

@davidbella
davidbella / config.ru
Created October 21, 2013 13:43
Ruby: Basic Rack app with env and lambda
class RackApp
def call(env)
[200, {"Content-Type" => "text/html"}, ["Hello Rack! - you requested #{env["REQUEST_PATH"]}"]]
end
end
run RackApp.new
# lambda.call -- rack uses .call
#run lambda { |env| [200, {"Content-Type" => "text/html"}, [env.to_s]] }
class Integer
def to_roman
numeral = ""
number = self
hash = {1000 => "M", 900 => "CM", 500 => "D", 400 => "CD", 100 => "C", 90 => "XC", 50 => "L", 40 => "XL", 10 => "X", 9 => "IX", 5 => "V", 4 => "IV", 1 => "I"}
hash.each do |key, value|
until number < key
numeral << value
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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