Skip to content

Instantly share code, notes, and snippets.

@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 / 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!
class CodeWriter
VALID_OPTIONS = %w[
q
r
s
t
u
v
w
x
@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",
@epitron
epitron / _table_of_package_items.rhtml
Created September 17, 2012 03:56
Demo of "easy_table()" (a DSL for turning a collection of Ruby objects into an HTML table), created for a Rails app in 2006.
<%= easy_table(items, :class=>"table-style1", :tbody_id=>@table_id) do
col("Name") {|o| o.drug_price.name}
col("Qty", :quantity)
col("Price") {|o| number_to_currency(o.price) }
col("Actions") do |o|
link_to image_tag('small/delete.png'),
:action => "return_package_item",
:package_item => o.id,
@epitron
epitron / spec_helper.rb
Created September 15, 2012 18:11
How to use pry-rescue in your specs.
require 'spork'
Spork.prefork do
require 'rspec'
require 'pry'
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
@epitron
epitron / bench
Created September 10, 2012 06:35
Faster 'require' method
echo -n "with: "
time ruby -I. -rfast-require -e 'require "epitools"'
echo -n "without: "
time ruby -e 'require "epitools"'
@epitron
epitron / pry-ri.rb
Created April 13, 2012 08:42
Faster RI, with Pry's pager.
Pry.commands.command "ri", "RI it up!" do |*names|
# lazy loading
require 'rdoc/ri/driver'
unless RDoc::RI::Driver.responds_to? :monkeypatched?
class RDoc::RI::Driver
def page
lesspipe {|less| yield less}
end
@epitron
epitron / gist:1872524
Created February 21, 2012 00:23
cirwin's WTF command
Pry.commands.command(/^wtf([?!]*)/, "show backtrace") do |arg|
raise Pry::CommandError, "No most-recent exception" unless _pry_.last_exception
output.puts _pry_.last_exception
output.puts _pry_.last_exception.backtrace.first([arg.size, 0.5].max * 10)
end
@epitron
epitron / blackcarpet.rb
Created January 30, 2012 22:30
BlackCarpet (ANSI renderer for RedCarpet)
require 'epitools'
require 'redcarpet'
require 'coderay'
def indented?(text)
indent_sizes = text.lines.map{ |line| if line =~ /^(\s+)/ then $1 else '' end }.map(&:size)
indent_sizes.all? {|dent| dent > 0 }
end
def unwrap(text)