Skip to content

Instantly share code, notes, and snippets.

@epitron
epitron / human_readable.rb
Created November 10, 2012 08:33
Bytes to human-readable format.
class Numeric
SIZE_TABLE = {
# power # units
0 => "",
1 => "KB",
2 => "MB",
3 => "GB",
4 => "TB",
5 => "PB",
class CodeWriter
VALID_OPTIONS = %w[
q
r
s
t
u
v
w
x
@epitron
epitron / output.txt
Created January 19, 2013 01:37
Magic block-taking require method.
[08:35 PM] epi@yew :: ~/code/pry $ pry
Loading gems...
|_ epitools
|_ coderay
|_ awesome_print
|_ print_members
[1] pry(main)> whee!
@epitron
epitron / cpux86-ta.js
Created February 6, 2013 20:34
JSLinux (Javascript x86 emulator)
/*
PC Emulator
Copyright (c) 2011 Fabrice Bellard
Redistribution or commercial use is prohibited without the author's
permission.
*/
"use strict";
var aa = [1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1];
@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'
@epitron
epitron / text_carousel.coffee
Created February 16, 2013 18:42
A simple Carousel in CoffeeScript.
@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 / 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 / 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 / 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