Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar
💭
I may be slow to respond.

Chris Bloom chrisbloom7

💭
I may be slow to respond.
View GitHub Profile
@chrisbloom7
chrisbloom7 / ex_passing_slices_and_arrays_by_val.go
Created June 4, 2019 00:57
Surprising behavior when passing arrays and slices to functions in Go
package main
import (
"fmt"
)
var a1 = [3]int{1, 2, 3}
var s1 = a1[0:3]
func main() {
@chrisbloom7
chrisbloom7 / prime.rb
Created May 22, 2019 16:31
Detecting prime numbers in Ruby
# A prime is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers
def prime?(int)
# A prime is a natural number greater than 1...
return false if int < 2
# Is _int_ evenly divisible by any number 2 -> int-1?
(2..(int - 1)).each do |divisor|
return false if int % divisor == 0
end
return true

Doubly Linked List implementation in Ruby

Setup

  • Install Ruby ~> 2.6
  • gem install rspec
  • Open an IRB session with irb -r ./linked_list.rb
list = LinkedList.new
@chrisbloom7
chrisbloom7 / I18n for Panini.md
Last active April 9, 2019 21:10
Adding a MailChimp Template Language (MTL) based I18n helper for Panini templates

i18n for Panini

Requirements

Other than the default Panini pieces, you will also need to make sure that lodash is included as a devDependencies in your package.json

Usage

Tag a section of text as translatable. The section will be replaced with a set of if/elseif/else Merge Tags for each translation provided, otherwise only the wrapped content is displayed.

@chrisbloom7
chrisbloom7 / A ruby module to assist in capturing stdout and stderr from a detached thread in jRuby.md
Last active April 9, 2019 21:07
Ruby module to assist in capturing stdout/stderr from a detached thread

A ruby module to assist in capturing stdout/stderr from a detached thread in jRuby

  1. create some temp files (but not Tempfiles since they would be GC'd too quickly)
  2. append standard file descriptor redirects to the commands that we'll run in the detached spawned process, i.e. 1>>stdout_tempfile_path 2>>stderr_tempfile_path
  3. tack on a final command that will trigger copying the files to S3 regardless of exit status of original commands, i.e. {original commands with io redirection &&'d together}; bundle exec rake cleanup_task
  4. cleanup other io redirection temp files older than some threshold

Example usage:

@chrisbloom7
chrisbloom7 / .rails_aliases
Last active January 31, 2019 16:15
Bash shortcuts for routing Rails commands through the binstubs if they are present, or fallback to the other version-dependent methods
# RUBY / RUBY ON RAILS COMMANDS
alias bexec='bundle exec'
alias rails_mv="bexec rails -v | sed 's/Rails \([0-9]\).*/\1/g'"
# Alias the rake command to Spring binstubs or fallback to "bundle exec"
# http://goo.gl/HkhHAf, http://goo.gl/STtIvF
function brake {
if [ -f bin/rake ]
then
bin/rake "$@"
else
@chrisbloom7
chrisbloom7 / .bash_aliases
Last active June 23, 2018 00:37
A portion of my alias entries and some other useful snippets for Bash
#!/bin/sh
# GENERAL COMMANDS
alias l='ls -AHhlp'
alias c='clear'
alias cx='chmod +x'
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
alias reload='source ~/.bash_profile'
alias release='xattr -d com.apple.quarantine'
alias flushdns='sudo discoveryutil udnsflushcaches'
@chrisbloom7
chrisbloom7 / example_table.html
Created June 11, 2012 18:52
A cucumber/capybara step to test the order of data in a table, either with or without headers
<html>
<body>
<section class="left">
<article id="customers">
<h2>New Customers</h2>
<table class="data">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
@chrisbloom7
chrisbloom7 / meet_chef_notes.md
Created March 19, 2014 19:55
Notes from the Meet Chef video tutorial at http://pluralsight.com/
@chrisbloom7
chrisbloom7 / README.md
Last active October 31, 2016 04:28
Bust AJAX request caching

The following will prevent all future AJAX requests from being cached, regardless of which jQuery method you use ($.get, $.ajax, etc.)

$.ajaxSetup({ cache: false });

Source: http://stackoverflow.com/a/735101/83743

However, jQuery's AJAX object will follow redirects, so even if you disable caching globally the "_={timestamp}" parameter that jQuery adds to bust the browser's cache may not be forwarded with the redirect. In that case, your request can still be cached by the browser. The solution is to either make sure that special param is passed along with redirects, or to send the appropriate cache-busting response headers from the server-side code for those requests.

Reference: http://api.jquery.com/jQuery.ajax/