Skip to content

Instantly share code, notes, and snippets.

View dmgarland's full-sized avatar

Dan Garland dmgarland

View GitHub Profile

Keybase proof

I hereby claim:

  • I am dmgarland on github.
  • I am dmgarland (https://keybase.io/dmgarland) on keybase.
  • I have a public key ASDXl6fdEW8xby6al3hPhXfusDPoB7dTXBhLlzNEqV7VYgo

To claim this, I am signing this object:

# The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
# Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
# (Please note that the palindromic number, in either base, may not include leading 0's)
# Naive approach - TODO: needs to avoid checking both base 2 and base 10 if base 10 isn't pallindromic
def palindromic?(n, base = 10)
numbers = n.to_s(base).split("")
middle = numbers.length / 2.0
first_half = numbers.slice(0, middle.ceil)
second_half = numbers.slice(middle.floor, numbers.length)
<!DOCTYPE html>
<html>
<head>
<title>Underscore example</title>
<script src="underscore.js"></script>
</head>
<body>
<div id="result"></div>
@dmgarland
dmgarland / rake_task_patch.rb
Created May 15, 2015 13:47
Here's a monkey patch I found on a client project. What does it do? It takes up a day of a Rails developer's time. DONT DO THIS!
# encoding: utf-8
require 'rake/task'
module Rake
class Task
alias :orig_execute :execute
def execute(args=nil)
orig_execute(args)
Country.create(iso: 'AF', name: 'Afghanistan', iso3: 'AFG')
Country.create(iso: 'AL', name: 'Albania', iso3: 'ALB')
Country.create(iso: 'DZ', name: 'Algeria', iso3: 'DZA')
Country.create(iso: 'AS', name: 'American Samoa', iso3: 'ASM')
Country.create(iso: 'AD', name: 'Andorra', iso3: 'AND')
Country.create(iso: 'AO', name: 'Angola', iso3: 'AGO')
Country.create(iso: 'AI', name: 'Anguilla', iso3: 'AIA')
Country.create(iso: 'AG', name: 'Antigua and Barbuda', iso3: 'ATG')
Country.create(iso: 'AR', name: 'Argentina', iso3: 'ARG')
Country.create(iso: 'AM', name: 'Armenia', iso3: 'ARM')
@dmgarland
dmgarland / index.html
Created May 19, 2014 10:07
Underscore template example (HTML)
<html>
<head>
<title>Underscore Howto</title>
<script type="text/javascript" src="javascripts/jquery-2.1.1.js"></script>
<script type="text/javascript" src="javascripts/underscore.js"></script>
<script type="text/javascript" src="javascripts/my_amazing_javascript.js"></script>
</head>
<body>
<h1>Underscore / JQuery practice</h1>
@dmgarland
dmgarland / gist:a41f0fbf75f1edce0120
Created May 8, 2014 11:00
Starting point for a authors controlller...
PadrinoBlog::App.controllers :authors do
get :new do
@author = Author.new
erb :'authors/new'
end
post :create do
@author = Author.new(params[:author])
@dmgarland
dmgarland / gist:61a83147681069d6b4f2
Created May 1, 2014 10:09
Sinatra session / login example.
require 'sinatra'
enable :sessions
before do
if request.path != '/login' && !session[:logged_in]
redirect '/login'
end
end
class MyAmazingWebApp
@@headers = {
"Content-Type" => "text/html",
"My-Amazing-Header" => "coolbeans"
}.freeze
def call(env)
begin
[200, @@headers, File.open("public#{env['PATH_INFO']}")]
@dmgarland
dmgarland / sanity.rb
Created March 25, 2014 17:29
URL checker
require 'pry'
require 'httparty'
failed = []
File.open('urls.csv', 'r').each_with_index do |line, number|
next if number == 0
path = line.split(",").first
url = "http://localhost:9293#{path}"
puts url