Skip to content

Instantly share code, notes, and snippets.

View pmn4's full-sized avatar

Pat Newell pmn4

View GitHub Profile
@pmn4
pmn4 / JSON.stringify
Created April 10, 2014 14:45
JSON.stringify takes Parameters
var o = {
a: 1,
b: 2,
c: 3,
d: {
a: 5,
b: 6
}
};
@pmn4
pmn4 / service_response_object.rb
Last active August 29, 2015 14:13
Ruby Classes as Hash Wrappers (for faster serialization)
#!/usr/bin/env ruby
# More often than not at @RTR_tech, our ruby layer is a pass-through from our
# services to our client application. In those cases, it doesn't make much
# sense to parse the JSON string.
#
# Other times, however, we want to work with the objects that come back, so
# like good ruby developers, we create classes which reflect the data we
# expect, but as an intermittant step, we convert the JSON to a Hash, which
# we convert to an object.
@pmn4
pmn4 / gist:ccb5bef02743052e14e7
Last active August 29, 2015 14:13
TIL: Backbone.Collection#parse
/*
let's say you are trying to inflate a Backbone Collection
using some endpoint which returns data in non-array form.
maybe,
*/
{
meta: {
total: 25,
pageSize: 3,
@pmn4
pmn4 / gist:1c977e32b39f5bba7432
Last active August 29, 2015 14:15
AngularJS Ordinal Filter
// convert numbers to ordinal versions of themselves
// {{ 1 | ordinal }} : "1st"
// {{ 2 | ordinal }} : "2nd"
// {{ 3 | ordinal }} : "3rd"
// etc.
angular
.module("app", [])
.filter("ordinal", function () {
var suffix = ["th", "st", "nd", "rd"];
@pmn4
pmn4 / gist:a8d9547bbf08d23d893e
Created February 19, 2015 17:01
Ruby/Sinatra Refactor for Code Climate
# Original Code
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / gist:7e4897bbb020201fc10a
Created February 19, 2015 17:01
Ruby/Sinatra Refactor for Code Climate
# Original Code
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / gist:189aa7558aec17989c00
Created February 19, 2015 17:01
Ruby/Sinatra Refactor for Code Climate
# Original Code
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / 1-original-impementation.rb
Last active August 29, 2015 14:15
Modular Sinatra Refactor vs Code Complexity
class App < Sinatra::Base
get '/users' do
User.all.to_json
end
post '/users' do
User.create!(params)
204
end
@pmn4
pmn4 / Complex Conditionals
Created August 23, 2017 14:34
Breaking complex conditional statements into more a readable/commentable form yields Code Climate warnings.
// to increase readability and to allow for more sane and detailed
// commenting, I prefer to break up complex conditional statements
// into separate statements in a separate function, using `return`
// to short-circuit when a condition has failed
/////////////////////////////////////////////////////////////////
// as an alternative to combining the condition and the behavior:
// ❌❌❌
function render() {
if (
@pmn4
pmn4 / min_change_input_pair.py
Created January 1, 2018 20:08
Suppose you are given a list of possible Bitcoin that you control (inputs). You need to pay someone exactly 0.71 BTC. How would you select exactly 2 inputs in such a way as to minimize the change output if you could ignore fees? Write a python function that selects the two inputs.
# simple class to contain the id and value of a transaction
class Transaction:
def __init__(self, id, value):
self.id = id
self.value = value
def __add__(self, other):
return self.value + other.value
# readable output