Skip to content

Instantly share code, notes, and snippets.

@epitron
epitron / tweet-anagrams.rb
Last active December 22, 2015 13:28
Find tweets that are anagrams of each other.
require 'set'
require 'pp'
require 'date'
require 'pry'
class Index
include Enumerable
def initialize
@epitron
epitron / binding-addition.rb
Last active December 19, 2015 20:49
Add bindings together!
class Binding
def +(other)
self.eval do
other.eval do
binding
end
end
end
end
@epitron
epitron / jquery-throttle.js
Created July 5, 2013 19:42
Throttle repeated JavaScript events (good for making your autocomplete not try to query the server on every keystroke). Not by me!
/*!
* jQuery throttle / debounce - v1.1 - 3/7/2010
* http://benalman.com/projects/jquery-throttle-debounce-plugin/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// Script: jQuery throttle / debounce: Sometimes, less is more!
def notify(msg)
system("notify-send", msg)
end
Signal.trap("HUP") {
3.downto(1) { |n| notify("#{n}..."); sleep 1 }
notify("DEATH!")
exit
}
@epitron
epitron / webfile.rb
Created May 30, 2013 03:46
A class that emulates a File object, but over HTTP.
require 'net/http'
require 'uri'
module URI
def ssl?; scheme == 'https'; end
end
class WebFile
@epitron
epitron / pry-doc_coverage.rb
Created April 7, 2013 03:23
Uses Pry to shows undocumented methods of Ruby classes.
require 'pry'
ObjectSpace.each_object do |o|
o.methods(false).each do |meth|
name = "#{o.class}##{meth}"
result = Pry::Method.from_str(name)
if result.nil?
puts "#{name}"
@epitron
epitron / enum_weave.rb
Last active December 15, 2015 15:09
Weave together multiple sorted enumerables into one sorted enumerable. (The supplied block is what to sort by, and an enumerable is returned.)
def weave(*enums, &key_getter)
enums = enums.map { |e| e.to_enum.each }
Enumerator.new do |yielder|
while enums.any?
enums.sort_by! { |enum| key_getter.call(enum.peek) rescue 0 }
begin
yielder << enums.first.next
@epitron
epitron / fastwords.rb
Last active December 14, 2015 17:28
Finds words that can be spelled using a given set of letters. (aka. Scrabble assistant. :D)
require 'pp'
class Word < Hash
attr_accessor :original
def initialize(word)
@original = word
h = super(0)
word.chars.each { |c| h[c] += 1 }
@epitron
epitron / text_carousel.coffee
Created February 16, 2013 18:42
A simple Carousel in CoffeeScript.
@epitron
epitron / base64_fixed_point.rb
Last active December 12, 2015 07:28
Compute a Base64 fixed-point.
###########################################################################
# Base64 Fixed-point Calculator
###########################################################################
SEED = ":D" # The initial string to Base64 encode
AMOUNT = 1500 # How many bytes should the fixed point end up being?
###########################################################################
require 'base64'