Skip to content

Instantly share code, notes, and snippets.

View cyberfox's full-sized avatar

Morgan Schweers cyberfox

View GitHub Profile
require 'rubygems'
require 'sqlite3'
require 'sequel'
# Adding this monkey-patch makes it work on MacRuby.
module Sequel
class Dataset
def single_record
record = nil
clone(:limit=>1).each{|r| record = r; return r}
@cyberfox
cyberfox / canvas_test.html
Created May 4, 2011 00:11
A simple <canvas> element test
<!DOCTYPE html>
<html>
<head>
<title>Testing HTML5 Canvas element</title>
<script src="jquery.min.js" type="text/javascript"></script>
</head>
<body>
This is a test of the HTML5 Canvas element.
<canvas id="drawing_canvas" width="800" height="600" style="position: absolute; top: 0; left: 0;">
</canvas>
@cyberfox
cyberfox / keychain.rb
Created September 18, 2011 06:03
Convert OS X Keychain exported entries into logins for 1Password import
#!/usr/bin/env ruby
#
# Usage:
# security dump-keychain -d login.keychain > keychain_logins.txt
# # Lots of clicking 'Always Allow', or just 'Allow', until it's done...
@cyberfox
cyberfox / humanize.js
Created October 20, 2011 18:43
JavaScript humanize method.
com = { cyberfox: {} };
/**
* Convert a property name into a human readable string by replacing _ with
* spaces, and upcasing the first letter of each word.
*
* @param {string} property The property name to convert into a readable name.
* @return {string} The property name converted to a friendly readable format.
* @private
*/
@cyberfox
cyberfox / string_permutations.rb
Created January 7, 2012 09:11
Return all permutations of a string, without any duplicates.
require 'set'
# First I wanted to get an idea of the timing of the various approaches
def time
start = Time.now
yield
puts Time.now.to_f - start.to_f
end
# This optimizes for redundant strings of values, front-loading them to maximize duplicates
@cyberfox
cyberfox / champagne.rb
Created January 27, 2012 09:14
Champagne Solution
if ARGV[0].nil?
puts "Usage:\n\truby #{__FILE__} {bad bottle#|test}"
exit
end
def guess_bottle(bad_bottle, silent=false)
waiters = []
(0..9).each do |waiter_number|
waiters[waiter_number] = []
(0..999).each do |bottle|
@cyberfox
cyberfox / entity_weirdness.rb
Last active August 29, 2015 13:59
RubyMotion entity name weirdness
# This shows the list of entities, and that they look like String objects to RubyMotion.
(main)> mapping = App.delegate.managedObjectContext.persistentStoreCoordinator.managedObjectModel.entitiesByName.keys.each_with_object({}) { |key, map| map[key.to_s] = key}
=> {"Auction"=>"Auction", "Category"=>"Category", "Entry"=>"Entry", "Host"=>"Host"}
# This is an example lookup for the Category entity, by name. It fails.
(main)> App.delegate.managedObjectContext.persistentStoreCoordinator.managedObjectModel.entitiesByName['Category']
=> nil
# Here we try to insert a new object for an entity by name, using 'Category' as a raw string. It fails, unable to locate it.
(main)> begin
@cyberfox
cyberfox / binary.html
Created June 3, 2015 07:50
Binary entry HTML+CSS+JavaScript
<!DOCTYPE html>
<html>
<head>
<title>Binary Entry JavaScript</title>
<style type="text/css">
.box {
border: 1px solid black;
height: 32px;
width: 32px;
font-size: 24px;
@cyberfox
cyberfox / oauth_sinatra_client.rb
Created August 28, 2015 06:18
Simple Salesforce & Twitter OAuth2 Sinatra client
require 'sinatra'
require 'omniauth-twitter'
require 'omniauth-salesforce'
configure do
enable :sessions
end
helpers do
def h(text)
@cyberfox
cyberfox / post_test.rb
Created March 10, 2017 09:37
Demonstration of the problem with counter_cache in Rails 5 (and 4)
require 'test_helper'
# ActiveRecord::Schema.define(version: 20170310085807) do
# create_table "forums", force: :cascade do |t|
# t.string "title"
# t.integer "posts_count"
# t.datetime "created_at", null: false
# t.datetime "updated_at", null: false
# end
#