Skip to content

Instantly share code, notes, and snippets.

View DiegoSalazar's full-sized avatar

Diego E. Salazar DiegoSalazar

View GitHub Profile
@DiegoSalazar
DiegoSalazar / var_thread_test.rb
Created February 12, 2014 18:43
test variables are shared between threads
# testing out class variables are shared between instances
require 'thread'
class A
@m = Mutex.new
@@m = Mutex.new
M = Mutex.new
def self.m; @m end
def self.mm; @@m end
@DiegoSalazar
DiegoSalazar / hw0-part1.rb
Created May 15, 2014 21:46
ESAAS HW0 Part 1
# I iterate over ints using inject and pass in an accumulator
# which for the purposes of summing will have to be 0
# each number is added to the accumulator and the final result
# is returned
def sum(ints)
ints.inject(0) { |sum, i| sum += i }
end
# this was fun, I sort and mutate the ints array to maintain its
# newly sorted state, pop off the last value which will now be
@DiegoSalazar
DiegoSalazar / hw0-part1.rb
Created May 15, 2014 22:16
ESAAS HW0 Part 2
# using string interpolation
def hello(name)
"Hello, #{name}"
end
# I first check if the first letter is alphabetical
# then see if the first lower case letter is a vowel
# and return the opposite of that boolean
def starts_with_consonant?(s)
return false unless s[/^[a-z]/i]
@DiegoSalazar
DiegoSalazar / hw0-part3.rb
Created May 15, 2014 22:23
ESAAS HW0 Part 3
class BookInStock
# declare setters and getters
attr_accessor :isbn, :price
def initialize(isbn, price)
# checking for empty string or price is greater 0, raises exception
raise ArgumentError if isbn.strip.empty? || price <= 0
@isbn, @price = isbn, price
end
@DiegoSalazar
DiegoSalazar / each-with-else.rb
Created October 15, 2014 15:16
Ruby each with else
def for_each(items, do_this, options = { else: -> {} })
unless items.empty?
items.each &do_this
else
options[:else].call
end
end
items = [1,2,3]
for_each items, ->(i) {
@DiegoSalazar
DiegoSalazar / api_curl_test.sh
Created October 29, 2014 14:40
API request signature and test
#!/bin/bash
SECRET_KEY=$TEST_SENDER_KEY
ACCESS_ID=$TEST_SENDER_TOKEN
API_BASE=$TEST_ENDPOINT_BASE
query="{\"document\":{\"recipient_id\":\"$TEST_RECIPIENT_TOKEN\",\"data\":{\"id\":\"1\",\"first_name\":\"Bill\"}}}"
content_md5=$(echo -n $query | openssl md5 -binary | base64)
content_type='application/json'
@DiegoSalazar
DiegoSalazar / flatten_hash_recurse.rb
Created January 6, 2015 17:11
Flatten an arbitrarily nested hash into a flat hash with dotted key names
a = { a: 1, b: { c: 2, d: { e: 3 }}}
# define a recursive proc:
flatten_keys = -> (h, prefix = "") do
@flattened_keys ||= {}
h.each do |key, value|
# Here we check if there's "sub documents" by asking if the value is a Hash
# we also pass in the name of the current prefix and key and append a . to it
if value.is_a? Hash
flatten_keys.call value, "#{prefix}#{key}."
@DiegoSalazar
DiegoSalazar / find_hotspots.rb
Created February 6, 2015 03:00
Print out hotspots from some kind of data file
#!/usr/bin/env ruby
# from the command line run:
# ruby ./find_hotspots.rb FILE_NAME THRESHOLD
class Array
def to_ranges
compact.sort.uniq.inject([]) do |r,x|
r.empty? || r.last.last.succ != x ? r << (x..x) : r[0..-2] << (r.last.first..x)
end
@DiegoSalazar
DiegoSalazar / hide_when_no_scrollbars.js
Created February 11, 2015 17:22
Tiny jQuery plugin to hide/show an element when a target element gets scrollbars
$.fn.hideWhenNoScrollBars = function() {
return this.each(function() {
var el = $(this),
target = $(el.data('target'));
function hideWhenNoScrollbars(target, el) {
if (target.scrollHeight > target.clientHeight) { // has vertical scrollbars
el.show();
} else {
el.hide();
0 5 10 15 20
|-----|-----|-----|-----|
s-e---s--e--s-e---s----es
Intervals of 5 minutes
job start = s
job end = e
s should always correspond to a |