Skip to content

Instantly share code, notes, and snippets.

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 / 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]] }
@davidbella
davidbella / grains.rb
Created October 24, 2013 16:12
Ruby: Squaring numbers and the rice checkerboard problem
# There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.
# There are 64 squares on a chessboard.
# Write a program that shows
# - how many grains were on each square, and
# - the total number of grains
# ## For bonus points
@davidbella
davidbella / .vimrc
Created October 25, 2013 17:35
Vim: My .vimrc
set nocompatible
set number
set cursorline
set ruler
set showmatch
set autoindent
set smartindent
set smarttab
@davidbella
davidbella / spec_helper_with_simplecov.rb
Created October 29, 2013 18:46
RSpec: Good simplecov config using Guard notifier
require 'simplecov'
SimpleCov.start do
add_filter '/db/'
add_filter '/config/'
add_filter '/spec/'
add_filter '/public/'
add_filter '/lib/' # Feel free to write coverage for the StudentScrape
SimpleCov.at_exit do
SimpleCov.result.format!
@davidbella
davidbella / lcc_ar_migration.regex
Created October 30, 2013 18:22
Regex: Matches _lower_camel_case words for ActiveRecord migrations
http://regex101.com/r/mV8rA7
/(?:_([^_.]+))+?/g
01_create_cats.rb
02_create_all_the_dogs.rb
/(?:_([^_.]+))+?/g
(?:_([^_.]+)) Non-capturing Group 1 to infinite times [lazy]
_ Literal _
@davidbella
davidbella / capybara cheat sheet
Last active December 27, 2015 18:39 — forked from zhengjia/capybara cheat sheet
Capybara: Cheat Sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
/**
* Learn HTML & CSS from Scratch
* 4.1: Responsive Layouts
*/
/********** Device Styles **********/
/**
* Tablet Landscape: 1024px
*/
@davidbella
davidbella / server.js
Created January 10, 2014 02:25
Node: Simple web server with built in HTTP
var http = require('http');
var server = http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World');
response.end();
})
server.listen(3000);
@davidbella
davidbella / server_function.js
Created January 10, 2014 03:04
Node: Simple HTTP server, function separated for demonstration
var http = require('http');
function server_response(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World');
response.end();
};
var server = http.createServer(server_response);