Skip to content

Instantly share code, notes, and snippets.

@langsharpe
langsharpe / heredoc.rb
Created August 30, 2021 04:07
Ruby Heredoc Syntax for String Literals
# The one you want is
def method
string = <<~END
This format allows you to indent the contents of the string without including the indent in the value.
String interpolation works here.
END
$ bundle exec sidekiq
m,
`$b
.ss, $$: .,d$
`$$P,d$P' .,md$P"'
,$$$$$bmmd$$$P^'
.d$$$$$$$$$$P'
$$^' `"^$$$' ____ _ _ _ _
@langsharpe
langsharpe / pg_dump.sh
Last active March 28, 2017 23:37
psql shortcuts
# Dump into a "custom" compressed format
pg_dump -Fc db_name > db_name.dump
@langsharpe
langsharpe / shell.sh
Created June 14, 2016 07:54
Bash options
set -e # Exit immediately if a command exits with a non-zero status
set -u # Exit immediately if an undefined variable is used (e.g. echo "$UNDEFINED_ENV_VAR")
set -o pipefail # Ensures pipelines (e.g. cmd | othercmd) return a non-zero status if a command fails
set -x # Print each expanded command before executing (useful for debugging)
# Source: https://buildkite.com/docs/guides/writing-build-scripts
@langsharpe
langsharpe / progress_countdown.rb
Created June 2, 2016 03:51
Progress countdown
# > ProgressCountDown.new.call { Model.where(state: [:pending, :waiting]).count }
# Things: |============== | (30693/224796) ETA: 03:37:1
class ProgressCountDown
def call
total = yield
progress =
ProgressBar.create(
title: "Things",
total: total,
@langsharpe
langsharpe / lhm.txt
Last active February 29, 2016 00:22
How the Lhm Gem works
https://github.com/soundcloud/lhm/blob/master/lib/lhm.rb#L45
https://github.com/soundcloud/lhm/blob/master/lib/lhm/invoker.rb#L43
https://github.com/soundcloud/lhm/blob/master/lib/lhm/entangler.rb
@langsharpe
langsharpe / elasticsearch_cheat.sh
Last active August 29, 2015 14:06
Elasticsearch cheat sheet
# Number of documents in an index
curl -s http://localhost:9200/test_volunteering_entities/_stats/docs | jq '._all.total.docs.count'
# First ten items in an index
curl -s http://localhost:9200/development_charities/_search?size=10 | jq '.hits.hits[] | ._source.name'
@langsharpe
langsharpe / derp.rb
Last active August 29, 2015 13:55
Does this pattern have a name?
class Derp
def self.do_the_thing(param)
new(param).do_the_thing
end
def do_the_thing(param)
# Actually does the thing
end
end
@langsharpe
langsharpe / gist:5268703
Last active December 15, 2015 13:39
Optimized for Happiness.
~/Dropbox/Projects/prospectsearch/prospectsearch(master ✗) rake spec
rake aborted!
undefined method `[]' for nil:NilClass
/Users/lang/.rvm/gems/ruby-1.9.3-p385@prospectsearch/gems/activerecord-3.2.13/lib/active_record/railties/databases.rake:525:in `block (3 levels) in <top (required)>'
/Users/lang/.rvm/gems/ruby-1.9.3-p385@global/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/Users/lang/.rvm/gems/ruby-1.9.3-p385@global/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/Users/lang/.rvm/gems/ruby-1.9.3-p385@global/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/Users/lang/.rvm/gems/ruby-1.9.3-p385@global/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/Users/lang/.rvm/gems/ruby-1.9.3-p385@global/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/Users/lang/.rvm/gems/ruby-1.9.3-p385@global/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
@langsharpe
langsharpe / hash.rb
Created March 20, 2011 04:23
Unordered Hash in Ruby 1.8
a = Hash.new
a['key1'] = 'one'
a['key0'] = 'zero'
a.each_value do |value|
puts value
end