Skip to content

Instantly share code, notes, and snippets.

require 'spec/environment_spec_helper'
describe 'UsersController' do
describe 'GET /new' do
it { expect(:get => '/users/new').to be_routable }
it {
get '/users/new'
expect(last_response.status).to eq 200
}
def with_model(m, &block)
w = Object.new
w.define_singleton_method :model do
m
end
w.instance_eval &block
end
def property(name)
puts model.to_s + name.to_s
def outer(&b)
foo = 1
block.call
end
def inner(v)
puts foo + v
end
# Is there a way to make 'foo' available in the scope of `inner`?
@fj
fj / gist:9691518
Last active August 29, 2015 13:57
"Your Mission", by Ellen M. H. Gates (1865)
If you cannot, on the ocean, sail among the swiftest fleet,
Rocking on the highest billows, laughing at the storms you meet,
You can stand among the sailors, anchored yet within the bay,
You can lend a hand to help them, as they launch their boats away.
If you are too weak to journey up the mountain steep and high,
You can stand within the valley, while the multitudes go by;
You can chant in happy measure, as they slowly pass along;
Though they may forget the singer, they will not forget the song.
@fj
fj / bf.rb
Created March 18, 2014 18:12
Brainfuck interpreter in Ruby
$_=$$/$$;@_=$_+$_;$-_=$_-@_
$__=->_{_==[]||_==''?$.:$_+$__[_[$_..$-_]]}
@__=->_,&__{_==[]?[]:[__[_[$.]]]+@__[_[$_..$-_],&__]}
$_____=->_{@__[[*_],&->__{__[$.]}]}
@_____=->_{@__[[*_],&->__{__[$-_]}]}
$______=->_{___,______=$_____[_],@_____[_];_____=$__[___];____={};__=$.;(_=->{
____[______[__]]=___[__];(__+=$_)==_____ ?____:_[]})[]}
@______=->_,__{_=[*_]+[*__];____=$__[_];___={};__=$.;(_____=->{
___[_[__][$.]]=_[__][$_];(__+=$_)==____ ?___:_____[]})[]}
$_______=->_{$___=[];@___=$__[_];__=___=____=$.;$____,@____={},[]
@fj
fj / gist:8541477
Created January 21, 2014 14:51
Hypothetical pattern-matching API in Ruby.
(1..100).match do |i|
pattern(i%3, i%5) {
match [0, 0], "FizzBuzz" # match i%3 == 0, i%5 == 0
match [0, _], "Fizz" # match i%3 == 0, i%5 == anything
match [_, 0], "Buzz" # match i%3 == anything, i%5 == 0
}
end
# => [nil, nil, "Fizz", nil, "Buzz", "Fizz", ...]
@fj
fj / gist:8393399
Created January 13, 2014 01:50
SymSpell fast-spellcheck algorithm from faroo.com. Original here: http://blog.faroo.com/2012/06/24/1000x-faster-spelling-correction-source-code-released/
// SymSpell: 1000x faster through Symmetric Delete spelling correction algorithm
//
// The Symmetric Delete spelling correction algorithm reduces the complexity of edit candidate generation and dictionary lookup
// for a given Damerau-Levenshtein distance. It is three orders of magnitude faster and language independent.
// Opposite to other algorithms only deletes are required, no transposes + replaces + inserts.
// Transposes + replaces + inserts of the input term are transformed into deletes of the dictionary term.
// Replaces and inserts are expensive and language dependent: e.g. Chinese has 70,000 Unicode Han characters!
//
// Copyright (C) 2012 Wolf Garbe, FAROO Limited
// Version: 1.6
# In a context somewhere, we'd like to do this:
u = User.new
u.extend Passworded
# Unfortunately, this doesn't work:
module Passworded
include ActiveModel::Model # Can't do this because ActiveModel::Model wants to be included
# on classes, not modules.
> class Naked; end
=> nil
> Naked.ancestors
=> [Naked, Object, Kernel, BasicObject]
> Naked.ancestors.count
=> 4
> require 'active_record'
alias.gl=log --format=format:"%C(yellow bold)%h%Creset @ %C(blue)%cd%C(reset) » %C(white)%s%C(reset) (%C(green)%cN%C(reset)) %+d" --topo-order --graph --date=iso