This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
OlderNewer