Skip to content

Instantly share code, notes, and snippets.


Diego E. Salazar DiegoSalazar

View GitHub Profile
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 =
@@m =
M =
def self.m; @m end
def; @@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 }
# 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}"
# 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
View each-with-else.rb
def for_each(items, do_this, options = { else: -> {} })
unless items.empty?
items.each &do_this
items = [1,2,3]
for_each items, ->(i) {
DiegoSalazar /
Created Oct 29, 2014
API request signature and test
content_md5=$(echo -n $query | openssl md5 -binary | base64)
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 value, "#{prefix}#{key}."
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)
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 = $('target'));
function hideWhenNoScrollbars(target, el) {
if (target.scrollHeight > target.clientHeight) { // has vertical scrollbars;
} else {
View job intervals desc
0 5 10 15 20
Intervals of 5 minutes
job start = s
job end = e
s should always correspond to a |