This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Javascript | |
// #1 | |
console.log(hello()); //=> undefined | |
function hello() {} | |
// #2 | |
console.log(hello()); //=> ReferenceError: hello not defined | |
hello = function() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleClass | |
@variable = "foo" | |
@@variable = "bar" | |
def initialize | |
@variable = "baz" | |
end | |
def self.test | |
puts @variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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| |