Skip to content

Instantly share code, notes, and snippets.

@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: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 / .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 / 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 / instapaper-backup.rb
Last active October 17, 2017 02:37
An Instapaper scraper.
#!/usr/bin/env ruby
require 'mechanize'
USERNAME = ""
PASSWORD = ""
# TODO: Save cookies with "http.cookie_jar.{load,save} filename"
# TODO: Store password in ~/.config or some kind of wallet
@epitron
epitron / http_client.rb
Created April 6, 2014 20:44
A wrapper around Mechanize which implements a caching HTTP client that doesn't blow up if it hits an error pages (like 404).
require 'mechanize'
require 'logger'
require_relative 'random_agent'
require_relative 'path'
# Mechanize::Page subclasses Mechanize::File, Mechanize::Download is its own thing
# module MimeInfo
# def size
# header["content-length"].to_i
require 'nokogiri'
require 'pp'
doc0 = %{
<p>Hello <b>DOM!</b></p>
}
doc1 = %{
<div>
<p>Hello <b>DOM!</b></p>
@epitron
epitron / gist:10345271
Last active August 29, 2015 13:58
OpenSSL codebase statistics.
[01:32 AM] epi@rebar :: ~/src/openssl-1.0.1g $ cloc .
2354 text files.
2135 unique files.
762 files ignored.
http://cloc.sourceforge.net v 1.60 T=11.68 s (127.0 files/s, 45241.9 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 915 32949 68512 234936
@epitron
epitron / webcam-rng.md
Last active August 29, 2015 14:00
A webcam CCD noise random number generator
@epitron
epitron / .motion.conf
Created May 29, 2014 21:20
Config file for 'motion', a program to record videos whenever there's motion (I used it for watching my birdfeeder with a webcam.)
# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 3.2.12
############################################################
# Daemon
############################################################
# Start in daemon (background) mode and release terminal (default: off)