Instantly share code, notes, and snippets.

Henrik Nyh henrik

View GitHub Profile
View gist:42ed93465e7d6299de54aef8636f2a84
<!DOCTYPE html>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
View tz_identifiers_to_rails_identifiers.rb
# blog post:
"Australia/Adelaide" => "Adelaide",
"Australia/Broken_Hill" => "Adelaide",
"America/Anchorage" => "Alaska",
"America/Juneau" => "Alaska",
"America/Nome" => "Alaska",
"America/Yakutat" => "Alaska",
"Pacific/Gambier" => "Alaska",
"Asia/Almaty" => "Almaty",
View post-commit
#!/usr/bin/env ruby
# diff-filter=AM = only show added and modified, not removed
changed_files = `git diff-tree --no-commit-id --name-only --diff-filter=AM -r HEAD`
unless changed_files.empty? # E.g. an "--allow-empty" commit.
bg_process = fork do
# This will report offenses in the entirety of the updated files, not just the changed lines. Not sure if we could easily get Rubocop to check changed-lines only, but this may be good enough.
rubocop_results = `bundle exec rubocop --color #{changed_files.join(" ")}`
View lgtv_turn_on.rb
# Based on: and
# Idea from:
# In order for this to work, first go to the TV "General" settings and turn on "Mobile TV On".
# Change this to the actual value.
# First find the TV IP, e.g. from your router's list of connected devices.
# Then run "arp -a" on a computer and you'll see the MAC by its IP.
# Maybe you need to "ssh <the ip>" before it will turn up in arp. Not sure.
TV_MAC_ADDRESS = "xx:xx:xx:xx:xx:xx"
View wip_private_ruby.vim
function! Snake()
ruby << EOF
b = Vim::Buffer.current
line_count = b.length
indexed_lines = { |i| [ i, b[i + 1] ] }
matches = { |i, line| line.match(/^\s+private$/) }
matches.each { |priv_i, priv_text|
re = /^#{priv_text.sub(" private", "end")}$/
View plug_conn_request_url.ex
defmodule Plug.Conn.RequestURL do
def request_url(conn) do
shown_port(conn.scheme, conn.port),
View rightmove_full_images.js
document.body.innerHTML = $("a[id^=thumbnail-] img").get().map(function(x) { return '<img src="' + x.src.replace(/_max_\d+x\d+/, "") + '" />' }).join()

You may have heard about cyber attacks in the news.

The data in <the service we provide> is well protected from attacks.

We also recommend that you protect your own computers, so you don't risk losing important data. You should do two things:

1. Back up important data. You can put important files in Dropbox or Google Drive, or back up your entire computer with something like Crashplan. In case of theft, catastrophy or the recent kind of cyber attacks, it's not enough to have backup only on your own machine (like Apple's "Time Machine"). You should have backups to "the cloud" (another computer on the Internet).

2. Keep your computers updated. Make sure automatic updates are turned on in Windows or macOS (also known as OS X). If your computer asks you whether it should be updated or restarted, make sure to do that within a few days. The recent cyber attacks could have been avoided if the computers had been u

View ruby_exhaustive_switch_case.rb
# By Henrik Nyh <> under the MIT license.
class Enum
class Switch
def initialize(value_to_match_against, *values)
@checked_values = { |v| [ v, false ] }.to_h
values.each do |value|
define_singleton_method(value) { |&block|
@checked_values[value] = true
# For use with dokku/dokku-postgres.
# Based on
set -e
mkdir -p $base_dir
ymd=$(date "+%Y-%m-%d")