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-info.md
Last active Sep 3, 2019
Automatically launch Cuphead (from Steam) on a Mac when a Nimbus Bluetooth game controller is connected.
View 1-info.md

The only game I play on the Mac Mini connected to our TV is Cuphead.

I wanted the game to launch automatically when I turn on one of our Nimbus game controllers, which connect via Bluetooth.

This is how:

  • Save the .scpt file below as e.g. ~/Library/Scripts/LaunchCupheadIfControllerIsConnected.scpt
    • In the .scpt, change "Nimbus" if your Bluetooth game controllers have some other name.
  • Save the .plist below as e.g. ~/Library/LaunchAgents/henrik.launch_cuphead_if_controller_is_connected.plist
    • In the .plist, change media to whatever your username is.
@henrik
henrik / bubblegin_recipe.md
Created Aug 15, 2019
Wetherspoon Bubblegin recipe
View bubblegin_recipe.md

Written down for the benefit of googlers, since I've tried to figure it out to make it at home.

This is my best guess from overhearing things and glancing behind the bar:

  • One part Zymurgorium "Realm of the Unicorn" gin
  • One part Funkin Pro bubblegum syrup
  • Add lemonade
  • Add ice and strawberries cut in half

I've tried the Imaginaria gin and I've tried the Monin bubblegum syrup, but they don't taste the same.

@henrik
henrik / ignore_exceptions_for_a_while.rb
Last active Jul 12, 2019
Ruby class using Redis to ignore exceptions inside a block unless they've consistently happened for the past X amount of time. Useful e.g. in recurring background workers: https://twitter.com/henrik/status/1149595443992485896
View ignore_exceptions_for_a_while.rb
class IgnoreExceptionsForAWhile
REDIS_KEY = "ignore_exceptions_for_a_while"
def self.call(exception_or_exceptions, name:, raise_after:, &block)
exceptions = Array(exception_or_exceptions)
key = "#{REDIS_KEY}:#{name}"
block.call
# If the block did not fail, reset this.
@henrik
henrik / spanish_organization_number.rb
Created Jun 27, 2019
Validate Spanish NIFs ("CIFs") in Ruby.
View spanish_organization_number.rb
# By "organization number" we mean an organization NIF (Spanish tax identification number). We don't mean personal NIFs such as DNI/NIE (see `SpanishIdentityNumber` for that).
#
# This class also supports personal NIFs starting with the letters K, L or M, though it's inaccurate to call these "organization numbers". More about these: https://translate.google.co.uk/translate?sl=auto&tl=en&u=https%3A%2F%2Fes.wikipedia.org%2Fwiki%2FN%25C3%25BAmero_de_identificaci%25C3%25B3n_fiscal
#
# It's easier to support them than not to, and means that any such users can provide this number, even though it's represented a bit inaccurately in our system. They're likely to be very rare. Feel free to think up a way to represent it more accurately. (Renaming `spanish_organization_number` to something like `non_dni_or_nie_spanish_tax_identification_number`?)
class SpanishOrganizationNumber
# NIFs do not have the letter prefixes "I" and "O" since those can be confused for the digits "1" and "0".
# Don't know why "T" is not a p
@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")}$/
You can’t perform that action at this time.