Skip to content

Instantly share code, notes, and snippets.

@danneu
danneu / anemone.rb
Created April 24, 2012 02:58
Crawls http://isbullsh.it and stores each blog post title & tag into MongoDB. RE: http://news.ycombinator.com/item?id=3878605
require 'anemone'
require 'mongo'
# Patterns
POST_WITHOUT_SLASH = %r[\d{4}\/\d{2}\/[^\/]+$] # http://isbullsh.it/2012/66/here-is-a-title (301 redirects to slash)
POST_WITH_SLASH = %r[\d{4}\/\d{2}\/[\w-]+\/$] # http://isbullsh.it/2012/66/here-is-a-title/
ANY_POST = Regexp.union POST_WITHOUT_SLASH, POST_WITH_SLASH
ANY_PAGE = %r[page\/\d+] # http://isbullsh.it/page/4
ANY_PATTERN = Regexp.union ANY_PAGE, ANY_POST
# Cache a func with multiple args like Math.max(1, 2, 3)
Function::cached = ->
cache = {}
(args...) => # Note: could've used a regular `->` and keep the `self = @`
argsKey = args.join()
if argsKey of cache
console.log "Hitting cache with #{argsKey}"
return cache[argsKey]
console.log "Cache miss..."
return cache[argsKey] = @(args...)
// Javascript
// #1
console.log(hello()); //=> undefined
function hello() {}
// #2
console.log(hello()); //=> ReferenceError: hello not defined
hello = function() {}
@danneu
danneu / ios.c
Created August 17, 2012 05:16 — forked from mahemoff/ios.c
iOS example
// Async web fetch with JSON parse
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"url"]]
queue:nil completionHandler:^(NSURLResponse *r, NSData *d, NSError *e) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:d
options:0
error:nil];
}];
// Using the camera
class ExampleClass
@variable = "foo"
@@variable = "bar"
def initialize
@variable = "baz"
end
def self.test
puts @variable
# db/migrate/20121019202052_create_posts.rb
class CreatePosts < ActiveRecord::Migration
def up
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
Post.create(title: "My first post", body: "And this is the post's content.")
Post.create(title: "How to lasso your dog",
@danneu
danneu / passwords_controller.rb
Created October 23, 2012 05:26 — forked from kazpsp/passwords_controller.rb
StrongParameters with Devise
# app/controllers/users/password_controller.rb
class Users::PasswordsController < Devise::PasswordsController
def resource_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
private :resource_params
end
require "em-proxy"
listen_point = { host: "0.0.0.0", port:8080 }
relay_point = { host: 'stream.twitter.com', port: 443 }
puts "Proxy listening"
Proxy.start(listen_point) do |conn|
conn.server :twitter, relay_point
conn.on_data { |data| data }
conn.on_finish { |backend, name| unbind }
conn.on_response { |backend, res|
print "."
@danneu
danneu / benchmark
Created October 29, 2012 23:04
Ox vs Nokogiri: DOM and SAX parsing comparison
# I'm no benchmark guru. Just did a bunch of:
$ time ruby <filename>
# Note: This is just an 80mb XML file with 38,000 nodes.
ox_dom.rb 4.56s user 0.78s system 93% cpu 5.714 total (550mb)
ox_dom.rb 4.58s user 0.79s system 87% cpu 6.126 total (550mb)
ox_dom.rb 4.60s user 0.80s system 87% cpu 6.140 total (550mb)
nokigiri_dom.rb 11.75s user 1.02s system 94% cpu 13.518 total (895mb)
nokigiri_dom.rb 11.36s user 1.02s system 93% cpu 13.211 total (895mb)
# documents controller
def show
@document = Document.find(params[:id])
@sections = @document.sections
end
# /views/document/show.erb
<h1>@document.name<h1>
@sections.each do |section|