Skip to content

Instantly share code, notes, and snippets.

Morgan Schweers cyberfox

Block or report user

Report or block cyberfox

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cyberfox
cyberfox / remove_dupes.rb
Last active Apr 2, 2017
Look for and remove duplicate files between a pair of trees that are similar.
View remove_dupes.rb
require 'digest/md5'
class FindDupes
def initialize(base)
@allfiles = Dir['**/**']
@base = base
@files = @allfiles.select {|f| File.file? f}
end
def find_current
@cyberfox
cyberfox / post_test.rb
Created Mar 10, 2017
Demonstration of the problem with counter_cache in Rails 5 (and 4)
View post_test.rb
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
#
@cyberfox
cyberfox / oauth_sinatra_client.rb
Created Aug 28, 2015
Simple Salesforce & Twitter OAuth2 Sinatra client
View oauth_sinatra_client.rb
require 'sinatra'
require 'omniauth-twitter'
require 'omniauth-salesforce'
configure do
enable :sessions
end
helpers do
def h(text)
@cyberfox
cyberfox / binary.html
Created Jun 3, 2015
Binary entry HTML+CSS+JavaScript
View binary.html
<!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 / entity_weirdness.rb
Last active Aug 29, 2015
RubyMotion entity name weirdness
View entity_weirdness.rb
# 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 / champagne.rb
Created Jan 27, 2012
Champagne Solution
View champagne.rb
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 / string_permutations.rb
Created Jan 7, 2012
Return all permutations of a string, without any duplicates.
View string_permutations.rb
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 / humanize.js
Created Oct 20, 2011
JavaScript humanize method.
View humanize.js
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 / keychain.rb
Created Sep 18, 2011
Convert OS X Keychain exported entries into logins for 1Password import
View keychain.rb
#!/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 / canvas_test.html
Created May 4, 2011
A simple <canvas> element test
View canvas_test.html
<!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>
You can’t perform that action at this time.