Skip to content

Instantly share code, notes, and snippets.

Justin Ko justinko

Block or report user

Report or block justinko

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View flatten_array.rb
require 'rspec/autorun'
class FlattenArray
def self.call(array)
new(array).call
end
def initialize(array)
@array = array
@flat_array = []
View keybase.md

Keybase proof

I hereby claim:

  • I am justinko on github.
  • I am jko170 (https://keybase.io/jko170) on keybase.
  • I have a public key whose fingerprint is 01F0 22A4 834B B3C4 FF22 4029 EEF7 D932 65EA 5E49

To claim this, I am signing this object:

@justinko
justinko / roman_numeral_calculator.rb
Last active Jun 4, 2017
Roman Numeral Calculator
View roman_numeral_calculator.rb
require 'rspec/autorun'
class RomanNumeralCalculator
SYMBOL_MAPPING = {
1 => 'I',
4 => 'IV',
5 => 'V',
9 => 'IX',
10 => 'X',
40 => 'XL',
View merge_string.rb
def merge_string(a, b)
[a, b].map(&:size).max.times.with_object('') do |(index), output|
[a, b].each {|var| output << (var[index] || '') }
end
end
@justinko
justinko / distributed_mutex.rb
Created May 9, 2017
Distributed Mutex with sidekiq-ent
View distributed_mutex.rb
class DistributedMutex
Blocked = Class.new(StandardError)
DEFAULT_OPTIONS = {
wait_timeout: 0,
lock_timeout: 30,
policy: :raise,
ttl: 1.hour
}
@justinko
justinko / realtime.rb
Created Jun 12, 2014
Ghetto Benchmark Function
View realtime.rb
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
@justinko
justinko / api_versioning.rb
Last active Aug 29, 2015
Inheritance based API versioning in Rails
View api_versioning.rb
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 / output.txt
Last active Dec 21, 2015
Parse streaming JSON
View output.txt
[3] pry(main)> streaming_json = StreamingJSON.new
=> #<StreamingJSON:0x007fe222b9d8e8
@buffer=
#<StreamingJSON::Buffer:0x007fe222b9d898
@complete=[],
@incomplete=[],
@nesting=#<StreamingJSON::Buffer::Nesting:0x007fe222b9d7d0 @level=0>,
@recording=false>>
[4] pry(main)> streaming_json[%({"a":)]
=> []
@justinko
justinko / mongo_aggregation.rb
Created Jun 10, 2013
Demonstrate MongoDB aggregation with the Ruby Mongoid gem.
View mongo_aggregation.rb
require 'mongoid'
require 'pp'
Mongoid.configure do |config|
config.allow_dynamic_fields = false
config.connect_to 'mongo_test', { consistency: :strong }
end
Mongoid.default_session.drop
View .rspec
--colour
-I app
You can’t perform that action at this time.