Skip to content

Instantly share code, notes, and snippets.

@showaltb
showaltb / define_method.rb
Created November 22, 2018 17:38
Example of using define_method in a mixin to define a method in an including class/module
module MyMixin
def self.included(other)
other.instance_eval do
define_method :bar do
puts 'bar called'
end
end
end
end
@showaltb
showaltb / proxy.yml
Created November 4, 2016 13:14
Ansible playbook to provision a tinyproxy server on an Ubuntu or CentOS (7.1+) host
# provision a tinyproxy server on an Ubuntu or CentOS (7.1+) host
- hosts: proxies
become: true
roles:
- geerlingguy.firewall
vars:
firewall_allowed_tcp_ports: [ 22, 8888 ]
#!/usr/bin/env perl
#
# A solution to http://blogs.perl.org/users/ovid/2014/12/a-small-puzzle-for-you.html
use Modern::Perl;
use Data::Printer multiline => 0;
my @a = ( 'b', 'c', 'f' );
my @b = ( 'a', 'd' );
my @c = ( 'c', 'd', 'e' );
@showaltb
showaltb / become_matchers.rb
Last active October 20, 2016 02:59
RSpec become, become_truthy, become_falsey matchers
# Matchers that will wait for a value to change.
# Tested with Ruby 2.3, RSpec 3.3
# Ex. expect { email.reload.delivered? }.to become_truthy
RSpec::Matchers.define :become_truthy do
supports_block_expectations
match do |block|
begin
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = block.call
@showaltb
showaltb / refactor_spec.rb
Last active May 4, 2016 16:09
Example of refactoring a spec
# BEFORE
describe '#remove_existing_ife_documents' do
it 'removes any existing documents on the item with the ife_id field set' do
item.documents << FactoryGirl.create(:document, document_type: document_type)
item.documents << FactoryGirl.create(:document, document_type: document_type, ife_id: '1234')
expect(item.documents.count).to eql(2)
ife_document_creator.send(:remove_existing_ife_documents)
expect(item.documents.count).to eql(1)
end
@showaltb
showaltb / remove-accidentally-added-file.md
Created August 1, 2015 14:25
Remove a file accidentally added with git commit

If you've created a git commit and accidentally included one or more files you didn't intend to, you can remove those files from the commit and place them back in the "Changed but not updated" or "Untracked files" state. Note: do this only if you have not pushed the commit, since this will rewrite history.

git reset HEAD^ file(s)...

git commit --amend -C HEAD

@showaltb
showaltb / bash_prompt.md
Last active May 23, 2023 17:51
Show changed or untracked files in bash prompt

Here is how to have your bash prompt tell you if you have changed or untracked files in your working directory.

Add these lines to ~/.bash_profile:

source /usr/local/etc/bash_completion
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
export PS1='\u@\h \w$(__git_ps1) \$ '
@showaltb
showaltb / gist:1ef9e196bc5ae404ef9c
Last active April 2, 2024 01:27
Handling Gemfile.lock conflicts during "git rebase"

If both the upstream and your feature branch have made changes to Gemfile, you will likely receive merge conflicts on Gemfile.lock when you rebase your feature branch. Don't try to resolve these manually; you'll probably just screw it up. Instead do this:

git checkout --ours Gemfile.lock
bundle
git add Gemfile.lock
git rebase --continue

This ensures that you get a correct Gemfile.lock at each step along the way.

@showaltb
showaltb / notes.rb
Last active December 16, 2015 16:20
Add Rspec and Cucumber to Rails "rake notes"
# add to lib/tasks/notes.rb:
task :configure_annotations do
Rails.application.configure do
config.annotations.directories.concat %w(spec features)
config.annotations.register_extensions('feature') { |tag| /#\s*(#{tag}):?\s*(.*)$/ }
end
end
task :notes => :configure_annotations
@showaltb
showaltb / gist:8542626
Created January 21, 2014 15:50
Install PhantomJS on Amazon AMI x86_64
# download and unpack distribution
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.6-linux-x86_64.tar.bz2
tar xf phantomjs-1.9.6-linux-x86_64.tar.bz2
# copy binary
cd phantomjs-1.9.6-linux-x86_64
cp bin/phantomjs /usr/local/bin