Skip to content

Instantly share code, notes, and snippets.

@marsbomber
marsbomber / parser.rb
Created October 22, 2011 11:34
Convert CSV to SQLite
require 'csv'
SOURCE_CSV = "data.csv"
OUTPUT_SQL = "#{Time.now.to_a.first(6).reverse.join}.sql"
OUTPUT_DB = OUTPUT_SQL + ".db"
data_array = CSV.read(SOURCE_CSV, :quote_char => "'", :col_sep =>',', :row_sep =>:auto)
header_array = data_array.delete_at(0).map { |h| h.downcase.gsub(" ", "_") }
header_string = header_array.join(", ")
@marsbomber
marsbomber / prime.rb
Created December 29, 2011 12:26
Prime numbers
#!/usr/bin/env ruby
def run max
return unless max > 1
return (2 .. max).inject([]) do |r, i|
if i.even?
r << i if i == 2
else
composite = false
@marsbomber
marsbomber / yaml_parse.rb
Created January 29, 2012 06:25
Ruby 1.9.3 YAML parse
require 'yaml'
yaml = <<yaml
defaults: &defaults
cool:
bang: wow
fruit: apple
development:
<<: *defaults
require 'spec_helper'
describe "RequestsDashboard", js: true do
let!(:req_processing_one) { create(:request, state: "processing", url: "processing-job-url-1") }
context "Processing requests" do
it "displays request details modal" do
find(:xpath, "//a[@href='#{job_request_admin_request_path(req_processing_one)}']").click
# this is passing on
@marsbomber
marsbomber / gist:4291868
Created December 15, 2012 07:06
Make up 100% CSS width from doing percentage calculation
I'll illustrate the problem with an example.
I query the database for some aggregated results. Say there are 4
aggregated counters I'm interested in and wanting to display them
in a 100% width div. Each of the counters should contribute to a
percentage of the full width.
For instance, my db query returns me somthing like this
| counter_1 | counter_2 | counter_3 | counter 4|
@marsbomber
marsbomber / gist:4325320
Created December 18, 2012 05:30
Given an array of INTs, calculate each INT's percentage contribution towards the sum of all INTs. Rounding needs to be done so that output floats can sum to a perfect 100%
def percentagerize(collection, result=[], remainder=1)
collection = Array(collection)
return result if collection.count == 0
denominator = collection.inject(:+)
percent = 0.0
unless denominator == 0
percent = (remainder * (collection[0] / denominator.to_f)).round(4)
@marsbomber
marsbomber / gist:4325345
Last active December 9, 2015 20:39
Rework on uni year one assignment question. Given any amount in cents, calculate changes
module ChangeMachine
class IdiotEncountered < StandardError; end
AVAILABLE_UNITS = [10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5]
def self.show_me_the_money(dollar_in_cents)
raise IdiotEncountered, "You are an idiot, no such amount in AUS..." unless dollar_in_cents % 5 == 0
dispense(dollar_in_cents)
@marsbomber
marsbomber / cloudSettings
Last active July 13, 2020 05:24
Visual Studio Code Sync Settings Gist
{"lastUpload":"2020-07-13T04:56:31.742Z","extensionVersion":"v3.4.3"}
// utils/partial.js
export const partial = (func, ...args) => func.bind(null, ...args);
// actions/maths.js
export const addNumberOneAndNumberTwo = (first, second) => {
return {
type: 'SOMETHING THE REDUCER CAN CATCH',
first,
second,
};
@marsbomber
marsbomber / createBatchActions.js
Created September 29, 2018 23:07 — forked from GuillaumeJasmin/createBatchActions.js
Batch actions async for redux-batched-actions
import { isPlainObject } from 'lodash';
import { batchActions as batchActionsDefault } from 'redux-batched-actions';
const handleInnerAction = (action, getState) => {
if (typeof action === 'function') {
return new Promise(resolve => {
const innerDispatch = innerAction => resolve(handleInnerAction(innerAction, getState));
action(innerDispatch, getState);
});
} else if (isPlainObject(action)) {