Skip to content

Instantly share code, notes, and snippets.

@epitron
epitron / irish-wotd.rb
Created April 6, 2014 19:55
Get a word of the day in Modern Irish Gaelic!
require 'mechanize'
http = Mechanize.new
page = http.get("http://www.focloir.ie/")
wotd_link = page.at(".wotdEntry a")["href"]
page = http.get(wotd_link)
puts page.at("#entryContent").text
@epitron
epitron / .pryrc
Last active January 4, 2016 12:49
Put Pry's history in different files (by date).
###################################################################
## History
PRY_CONFIG_DIR = File.expand_path("~/.pry")
Pry.history.loader = proc do |&block|
Dir["#{PRY_CONFIG_DIR}/history-*.log"].sort_by { |f| File.mtime f }.last(2).each do |fn|
File.foreach(fn) { |line| block.call(line) }
end
end
@epitron
epitron / gist:8252073
Last active January 2, 2016 04:39
Turning 'p' into a method that can be run on any object.
[1] pry(main)> module Kernel; alias old_p p; def p(*args); if args.any?; old_p(*args.dup); else; old_p(self); end; nil; end; end
=> nil
[2] pry(main)> p [1,2,3]
[1, 2, 3]
=> nil
[3] pry(main)> [1,2,3].each(&:p)
1
2
3
=> [1, 2, 3]
@epitron
epitron / pry-extra_underscores.rb
Last active January 1, 2016 16:09
Gives Pry _1, _2, _3, etc. variables (`_n` is equivalent to `_out_[n]`).
require 'binding_of_caller'
class Object
def method_missing(name, *args)
if name =~ /^_(\d+)$/
binding.of_caller(2).eval("self").output_array[$1.to_i]
else
super
end
end
@epitron
epitron / gist:8130324
Created December 26, 2013 05:59
rubygems 2.2.0.rc.1 is being weird... retrieving the gemspec for every version of json, from 1.5.5 and up, including java versions, until I manually stop it.
$ gem install -V pry-plus coolline
HEAD https://api.rubygems.org/latest_specs.4.8.gz
302 Moved Temporarily
HEAD https://s3.amazonaws.com/production.s3.rubygems.org/latest_specs.4.8.gz
304 Not Modified
HEAD https://api.rubygems.org/specs.4.8.gz
302 Moved Temporarily
HEAD https://s3.amazonaws.com/production.s3.rubygems.org/specs.4.8.gz
304 Not Modified
GET https://api.rubygems.org/quick/Marshal.4.8/json-1.5.5-java.gemspec.rz
@epitron
epitron / proxy_controller.rb
Created December 13, 2013 19:28
Turn a Rails controller into a web-proxy (web.archive.org-style)
require 'pp'
class ProxyController < ApplicationController
# Example headers:
# {"GATEWAY_INTERFACE"=>"CGI/1.1",
# "PATH_INFO"=>"/proxy/http:/google.com",
# "QUERY_STRING"=>"",
# "REMOTE_ADDR"=>"127.0.0.1",
# "REMOTE_HOST"=>"127.0.0.1",
@epitron
epitron / rle.rb
Last active December 30, 2015 01:49
Run-length encoding
def rle_regexp(s)
s.scan(/(.)(\1*)/).map {|c, extra| [c, extra.size+1] }
end
def rle_each_char(s)
result = []
last = nil
count = 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>
#define die(str, args...) do { \
// ==UserScript==
// @name YouTube: Click a username and see their videos!
// @namespace http://github.com/epitron/
// @version 0.1
// @description When you click a username, instead of seeing that lame youtube user's "summary" page, you get their actual list of videos (sorted by date).
// @include /^https?://(?:www\.)?(?:youtube\.com)/.*/
// @copyright 2013 epitron
// ==/UserScript==
(function() {
@epitron
epitron / epitest.rb
Last active December 23, 2015 11:29
A very powerful testing library.
class EpiTest
def self.inherited(base)
at_exit { base.new.run_my_tests_bitch! }
end
class Fail < Exception; end
def assert(thing)
raise Fail.new(caller_locations[0]) unless thing