Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

View GitHub Profile
@justinko
justinko / api_versioning.rb
Last active August 29, 2015 13:56
Inheritance based API versioning in Rails
class ApiVersioning
class Middleware
def initialize(app)
@app = app
end
def call(env)
if version_number = extract_version_number(env)
ApiVersioning.current_version_number = version_number.to_i
Rails.application.eager_load! unless Rails.application.config.cache_classes
@justinko
justinko / realtime.rb
Created June 12, 2014 21:56
Ghetto Benchmark Function
require 'benchmark'
def realtime(message = nil, count: 1, &block)
results = []
count.times { results << Benchmark.realtime(&block) }
puts
puts message if message
puts results.sum / results.size.to_f
puts
end
diff --git features/command_line/exit_status.feature features/command_line/exit_status.feature
index b0c23f8..070831f 100644
--- features/command_line/exit_status.feature
+++ features/command_line/exit_status.feature
@@ -47,3 +47,13 @@ Feature: exit status
"""
1 example, 1 failure
"""
+
+ Scenario: exit with 0 when no examples are run
# Instead of this:
name = 'Justin'
Person.where('name = ? OR display_name = ? OR cool_name = ? OR alias = ?', name, name, name, name)
# You can do
name = 'Justin'
Person.where('name = ? OR display_name = ? OR cool_name = ? OR alias = ?', *[name]*4)
class Raise
def self.raiser
1.join
end
def self.the_caller
raiser
rescue => exception
raise RuntimeError, "My custom message: #{exception.message}", exception.backtrace
end
- bar
- dog
- foo
- c:
- rat
- cat
- dog
- a
- b
@justinko
justinko / gist:773325
Created January 10, 2011 19:42
Subclassing a Ruby core class
class Storage
attr_reader :elements
def initialize
@elements = {}.tap do |elements|
elements[:people] = []
elements[:places] = []
end
end
@justinko
justinko / gist:773952
Created January 11, 2011 03:05
Create an HTML table in Ruby
html = "".tap do |str|
str << '<table>'
str << '<tr>'
str << '<td>'
str << 'a'
str << '</td>'
str << '<td>'
str << 'b'
str << '</td>'
str << '</tr>'
RSpec.configure do |config|
config.filter_run_excluding :remote => true
config.before :each, :remote => true do
# Configure code to hit the Braintree service
end
end
# Include your remote specs within your "normal" specs
describe Sweeper do
# Replaces `for_groups_matching`
RSpec.configure do |config|
config.shared :type => :model do
let(:foo) { 'only in models' }
end
end
# I'm actually against being able to call `shared`
# on the configuration instance. It's just not needed.
# You could achieve the same by doing this: