Skip to content

Instantly share code, notes, and snippets.

Avatar

Diego E. Salazar DiegoSalazar

View GitHub Profile
@DiegoSalazar
DiegoSalazar / var_thread_test.rb
Created Feb 12, 2014
test variables are shared between threads
View var_thread_test.rb
# 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
View hw0-part1.rb
# 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
View hw0-part1.rb
# 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]
View hw0-part3.rb
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
View each-with-else.rb
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 Oct 29, 2014
API request signature and test
View api_curl_test.sh
#!/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 Jan 6, 2015
Flatten an arbitrarily nested hash into a flat hash with dotted key names
View flatten_hash_recurse.rb
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 Feb 6, 2015
Print out hotspots from some kind of data file
View find_hotspots.rb
#!/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 Feb 11, 2015
Tiny jQuery plugin to hide/show an element when a target element gets scrollbars
View hide_when_no_scrollbars.js
$.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();
View job intervals desc
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 |
You can’t perform that action at this time.