Skip to content

Instantly share code, notes, and snippets.

@britishtea
britishtea / fish_right_prompt.fish
Last active February 17, 2024 22:20
My right prompt for the fish shell.
function fish_right_prompt -d "Write out the right prompt"
set -l exit_code $status
set -l is_git_repository (git rev-parse --is-inside-work-tree 2> /dev/null)
set -l max_shlvl 1; and test "$TERM" = "screen"; and set -l max_shlvl 2
# Print a fork symbol when in a subshell
if test $SHLVL -gt $max_shlvl
set_color yellow
echo -n "⑂ "
set_color normal
@britishtea
britishtea / benchmark.rb
Last active January 27, 2021 18:37
Array#join vs Regexp.union (ran on Ruby 2.0.0)
require "benchmark/ips"
input = %w[one two three four five six seven]
Benchmark.ips do |x|
x.report "Array#join" do
Regexp.new input.map { |e| Regexp.escape e }.join "|"
end
x.report "Regexp.union" do
@britishtea
britishtea / dependency.rb
Last active March 30, 2017 19:43
A null object in Ruby with refinements
module Dependency
def self.give_me_the_nil
nil
end
end
@britishtea
britishtea / _instructions.txt
Last active July 13, 2016 08:49
Run terco on login on Mac OS X
This are instructions to run terco on login on Mac OS X.
1. Install terco using the system ruby (sudo gem install terco).
2. Copy the script below to ~/Library/LaunchAgents/com.soveran.terco.plist. You may have to replace `/usr/local/bin/terco`, if terco has been installed to a different directory (use `which terco` to find out).
3. Load the script using the command
sudo launchctl load ~/Library/LaunchAgents/com.soveran.terco.plist
To make a domain resolve to localhost follow these instructions:
class Foo
def self.bar(&block)
instance_exec(&block)
end
attr_accessor :x
end
Foo.bar do
x = 5
require "simplecov"
SimpleCov.command_name "unit:#{Process.pid}"
SimpleCov.at_exit do
orig, $stdout = $stdout, File.open("/dev/null", "w")
SimpleCov.result.format!
$stdout, orig = orig, nil
end
SimpleCov.start
@britishtea
britishtea / jump.fish
Last active April 5, 2016 01:03
Bookmarking directories in the fish shell. Copy the file to $fish_function_path (~/.config/fish/functions by default) as jump.fish to install.
function jump -d "Jumps to a marked directory"
# If $JUMP_PATH isn't set, use a default (~/.config/jump).
if set -q $JUMP_PATH
set JUMP_PATH $HOME/.config/jump
end
# If the $JUMP_PATH directory doesn't exist, create it.
if not test -d $JUMP_PATH
mkdir -p $JUMP_PATH
end
class RingBuffer
attr_reader :capacity, :size
def initialize(capacity)
@array = Array.new(capacity)
@capacity = capacity
@read_index = 0
@size = 0
end
@britishtea
britishtea / example.rb
Last active December 31, 2015 16:09
Simple pattern matching in Ruby that won't win any beauty contests.
require 'function'
# A fibonacci function.
fib = Function.new
fib[0] = 0
fib[1] = 1
fib[Integer] = ->(i) { fib[i - 2] + fib[i - 1] }
p fib[0] # => 0
@britishtea
britishtea / example_case.rb
Last active December 25, 2015 12:49
Simple and possibly broken pattern matching in Ruby. Proof of Concept.
require "pattern_matching"
# Let's abuse case statements and the case equality method (#===) to implement
# pattern matching in Ruby.
#
# Note that this'll only work in Ruby 2.1.0-dev (2.0.0-p247 and lower does not
# honour refined #=== in case statements).
using PatternMatching