Skip to content

Instantly share code, notes, and snippets.

🦆 🦆 🦆 🦆

Henrik Nyh henrik

Block or report user

Report or block henrik

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
henrik / 1-exhaustive_case_exact_match.rb
Last active Apr 15, 2019
Experimental Ruby exhaustive conditionals that raise if no branch matches. A bit like in e.g. Elm.
View 1-exhaustive_case_exact_match.rb
# This replaces conditions like: `case …; when …; when …; else raise "Nothing matched!"` if an exact match is all you need.
def cond(input, hash)
hash.fetch(input) { raise "No branch for #{input.inspect}!" }.call
input = :good
good: -> { puts "So good!" },
bad: -> { puts "So bad!" },
View gist:42ed93465e7d6299de54aef8636f2a84
<!DOCTYPE html>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
henrik / tz_identifiers_to_rails_identifiers.rb
Created Aug 23, 2018 — forked from jpmckinney/tz_identifiers_to_rails_identifiers.rb
Maps tz database time zone identifiers to Rails time zone identifiers
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",
henrik / post-commit
Last active Nov 4, 2018
Git hook to run Rubocop on changed files after committing, without locking up the terminal. Bit of a WIP.
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(" ")}`
henrik / lgtv_turn_on.rb
Last active Nov 25, 2018
Ruby script to turn on a LGTV running WebOS 3, using "wake on LAN".
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"
henrik / wip_private_ruby.vim
Created Sep 2, 2017
WIP: Vim highlight private section of Ruby
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")}$/
henrik / plug_conn_request_url.ex
Last active Aug 23, 2017
Elixir Plug request_url function.
View plug_conn_request_url.ex
defmodule Plug.Conn.RequestURL do
def request_url(conn) do
shown_port(conn.scheme, conn.port),
henrik / rightmove_full_images.js
Created Jun 25, 2017
Bookmarklet to get all full size images on Rightmove.
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()
henrik /
Created May 15, 2017
The news bulletin about cyber attacks we sent to our company users.

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

henrik / ruby_exhaustive_switch_case.rb
Last active Aug 22, 2017
Ruby switch/case that complains if you forget to cover a case. Inspired by types in Elm. Just a for-fun experiment; not intended for real use.
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
You can’t perform that action at this time.