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
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
end
input = :good
cond(input,
good: -> { puts "So good!" },
bad: -> { puts "So bad!" },
View gist:42ed93465e7d6299de54aef8636f2a84
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.3.0.js"></script>
</head>
<body>
<script type="text/javascript">
dymo.label.framework.checkEnvironment();
</script>
@henrik
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: http://blog.slashpoundbang.com/post/2613268281/changing-from-tz-database-identifiers-to-rails-friendly
{
"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
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`.lines.map(&:chomp)
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
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
#!/usr/bin/ruby
# Based on: https://gist.github.com/dgv/2865897 and https://gist.github.com/zunda/187139
# Idea from: https://github.com/efpe/amazon-alexa-lg-tv
# 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
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 = line_count.times.map { |i| [ i, b[i + 1] ] }
matches = indexed_lines.select { |i, line| line.match(/^\s+private$/) }
matches.each { |priv_i, priv_text|
re = /^#{priv_text.sub(" private", "end")}$/
@henrik
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
Enum.join([
conn.scheme,
"://",
conn.host,
shown_port(conn.scheme, conn.port),
conn.request_path,
shown_qs(conn.query_string),
])
@henrik
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
henrik / cyberattacks_info.md
Created May 15, 2017
The news bulletin about cyber attacks we sent to our company users.
View cyberattacks_info.md

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
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 <https://henrik.nyh.se> under the MIT license.
class Enum
class Switch
def initialize(value_to_match_against, *values)
@checked_values = values.map { |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.