Skip to content

Instantly share code, notes, and snippets.

@pca2
pca2 / json_valid.rb
Last active August 29, 2015 14:03
Ruby JSON validator
#Borrowed from https://www.ruby-forum.com/topic/136579
require 'json'
def json_valid?(str)
JSON.parse(str)
return true
rescue JSON::ParserError
return false
end
@pca2
pca2 / return_min.rb
Created March 22, 2016 17:11
return smallest integer of an array
def return_min(array)
min = array[0]
for i in array
if i < min
min = i
end
end
return min
end
@pca2
pca2 / flatize.rb
Last active April 18, 2016 14:15
Flatten an array of arbitrarily nested arrays of integers
#! /usr/bin/env ruby
def flatize(array)
error_msg = 'Method only accepts nested arrays of integers'
raise ArgumentError, error_msg unless array.is_a? Array
flattened_array = []
array.each do |element|
if element.is_a? Array
flattened_array += flatize(element)
else
raise ArgumentError, error_msg unless element.is_a? Integer
@pca2
pca2 / check_md5sum.sh
Created April 18, 2016 15:53
Check the md5sums of two files
#!/bin/bash
check_md5sum(){
sums=$(md5sum $1 $2)
file_1=$(echo $sums | awk '{print $2}')
file_2=$(echo $sums | awk '{print $4}')
checksum_1=$(echo $sums | awk '{print $1}')
checksum_2=$(echo $sums | awk '{print $3}')
if [[ "$checksum_1" == "$checksum_2" ]]; then
echo "OK: $file_1 and $file_2 md5 Checksums Match"
echo $checksum_1 $file_1
@pca2
pca2 / make_change.rb
Created April 22, 2016 18:52
For given dollar amount make change (USA)
#!/usr/bin/env ruby
def make_change(change_amount)
#denominations to use. Could easily be made a parm.
#Note that these are integers, not floats
denominations = {
Penny: 1,
Nickel: 5,
Dime: 10,
Quarter: 25
@pca2
pca2 / benchmark_template.rb
Created August 6, 2016 15:42
Ruby Benchmarking Template
require 'benchmark'
#at its most basic, it looks like this:
Benchmark.bm do |bm|
bm.report {some code}
bm.report {some other code}
end
#A more robust example
iterations = 5
@pca2
pca2 / install-tmux.sh
Last active August 17, 2016 03:45 — forked from rothgar/install-tmux
Install tmux 2.0 on rhel/centos 6
# Install tmux on Centos release 6.5
# install deps
sudo yum install -y gcc kernel-devel make ncurses-devel
# DOWNLOAD SOURCES FOR LIBEVENT AND MAKE AND INSTALL
curl -OL https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar -xvzf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/local
@pca2
pca2 / date.js
Created December 29, 2016 04:07
Timestamp script key for Drafts iOS
// ex: 2016-12-28_230652
var now = new Date();
var dd = now.getDate();
var mm = now.getMonth()+1;
var yyyy = now.getFullYear();
var min = now.getMinutes();
var sec = now.getSeconds();
var hh = now.getHours();
@pca2
pca2 / dir.sh
Created March 1, 2017 17:43
bash containing directory
#return the containing directory of file
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
@pca2
pca2 / log.sh
Last active March 4, 2017 19:25
bash log function
log(){
datestamp=$(date +%Y-%m-%d_%H:%M:%S)
msg="$@"
echo "[$datestamp]- $msg"
}
#call with: log <whatever msg you want>