Skip to content

Instantly share code, notes, and snippets.

View JonRowe's full-sized avatar
💭
⛵️

Jon Rowe JonRowe

💭
⛵️
View GitHub Profile
@JonRowe
JonRowe / any_size_benchmark.rb
Created January 16, 2014 01:16
TIL that `empty?` is still significantly faster than `any?` despite `any?` being lazy.
require 'benchmark'
array = 10000000.times.to_a
puts "Any"
puts Benchmark.measure { 1000.times { array.any? } }
puts "Empty"
puts Benchmark.measure { 1000.times { array.empty? } }
@JonRowe
JonRowe / active.md
Last active December 28, 2015 15:08 — forked from parndt/active.md

Most active GitHub users (git.io/top)

The count of contributions (summary of Pull Requests, opened issues and commits) to public repos at GitHub.com from Sat, 17 Nov 2012 22:58:25 GMT till Sun, 17 Nov 2013 22:58:25 GMT.

Only first 1000 GitHub users according to the count of followers are taken. This is because of limitations of GitHub search. Sorting algo in pseudocode:

githubUsers
 .filter((user) -> user.followers > 30)
@JonRowe
JonRowe / better_better_specs.md
Created October 11, 2013 03:09
Whilst betterspecs.org has good ideas, it's examples are flawed.

Use contexts

Contexts are a powerful method to make your tests clear and well organized. In the long term this practice will keep tests easy to read.

I agree with the premise, and I agree that this:

BAD
it 'has 200 status code if logged in' do
1.9.3p448 :002 > a = [-> { puts 1; true }, -> { puts 2; false}, -> { puts 3; false }]
=> [#<Proc:0x007fe5c41a8f68@(irb):2 (lambda)>, #<Proc:0x007fe5c41a8f18@(irb):2 (lambda)>, #<Proc:0x007fe5c41a8ef0@(irb):2 (lambda)>]
1.9.3p448 :003 > a.all?(&:call)
1
2
=> false
1.9.3p448 :004 > a = [-> { puts 1; false }, -> { puts 2; true}, -> { puts 3; false }]
=> [#<Proc:0x007fe5c4276620@(irb):4 (lambda)>, #<Proc:0x007fe5c42765d0@(irb):4 (lambda)>, #<Proc:0x007fe5c42765a8@(irb):4 (lambda)>]
1.9.3p448 :005 > a.any?(&:call)
1
@JonRowe
JonRowe / benchmark.rb
Created August 20, 2013 11:35
Hash key lookup performance vs local variable.
require 'benchmark'
require 'stringio'
sizes = [10,100,1000,10000]
def measure(&block)
puts Benchmark.measure { 100000.times(&block) }
end
sizes.each do |size|
@JonRowe
JonRowe / capistrano.rb
Created August 18, 2013 10:23
Show broken sudo support in sprinkle pre/post hooks.
set :use_sudo, false
set :ssh_options, forward_agent: true
role :app, ENV['hostname'], primary: true
default_run_options[:pty] = true
@JonRowe
JonRowe / til.rb
Created August 8, 2013 03:16
TIL, local variables are scoped / defined even if never executed.
def test
begin
raise
local = 'b'
rescue
ensure
return defined?(local) || false
end
end
class Thing
def [] a, &block
block.call a
end
end
thing = Thing.new
lass One
2 def original arg
3 2
4 end
5 end
6
7 describe "one" do
8 it 'does things' do
9 one = One.new
10 one.stub(:original) do |arg|
@JonRowe
JonRowe / auth.rb
Last active December 17, 2015 08:29
Omniauth setup
Warden::Strategies.add(:login) do
def authenticate!
throw :warden
end
end
WardenOmniAuth.on_callback do |hash|
HandleAuthentication.new(hash).user
end