Skip to content

Instantly share code, notes, and snippets.

🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆 🦆 🦆 🦆
View GitHub Profile
henrik /
Last active October 26, 2022 22:41
Bash script to toggle the Sonos subwoofer via soco-cli.
# Uses via
# I map this to a HTPC keyboard key via
if [ `soco -l "Front room" sub_enabled` == "on" ]; then
soco -l "Front room" sub_enabled off : "Front room" light off
say -vDaniel "Sub off"
soco -l "Front room" sub_enabled on : "Front room" light on
say -vDaniel "Sub on"
View with_foo.rb
class Foo
def bar = "bar"
foo =
p foo.methods.include?(:bar) # => false
p foo.method(:bar) # Method
p Foo.instance_method(:bar) # Method
henrik / ruby_guard_clause_proof_of_concept.rb
Last active September 14, 2022 20:21
A for-fun vaguely Elixir-style guard clause proof-of-concept in Ruby. Probably not a good idea…
View ruby_guard_clause_proof_of_concept.rb
class Object
def guard(cond, meth)
prepend( {
define_method(meth) { |*a, **aa, &b|
super(*a, **aa, &b) if send(cond, *a, *aa, &b)
henrik / chunk_by_desired_max_value.rb
Created August 18, 2022 14:25
Similar to `Enumerable#chunk_while`, where it tries to make each chunk sum to no more than `max`.
View chunk_by_desired_max_value.rb
# Similar to `Enumerable#chunk_while`, where it tries to make each chunk sum to no more than `max`.
# If a value is greater than `max`, it becomes its own chunk. (This is the "desired" part – we don't reject values higher than `max`.)
class ChunkByDesiredMaxValue
def, max, &block)
block ||= -> { _1 }
sum = 0
enum.chunk_while { |a, b|
henrik / bigdecimal_extensions.rb
Last active August 3, 2022 13:55
View bigdecimal_extensions.rb
class BigDecimal
# `BigDecimal(123.456, 2)` becomes 120.0. It keeps 2 *significant digits*.
# If you want two *decimals* of precision, use this method.
def self.with_decimal_precision(value, precision)
if value.is_a?(Float)
length_before_decimal = Math.log10(value.abs).to_i + 1 # log for performance.
BigDecimal(value, length_before_decimal + precision)
BigDecimal(value, precision)
View daring_fireball.rb
require "open-uri"
r = -> { URI(_1).read }
post_dates = r.("")
.map { |(x)| Date.parse(x.gsub("&nbsp;", " ")) }
link_dates = r.("")
henrik / attr_extras_using_blocks_proof_of_concept.rb
Last active May 4, 2022 15:43
Proof of concept for a version of that uses block arguments to get lazy-loaded defaults.
View attr_extras_using_blocks_proof_of_concept.rb
module AttrExtras
def pattr_initialize(&block)
arg_names =
arg_names.each do
attr_reader _1
private _1
define_method(:__attr_extras_init_block, &block)
henrik / export_modified_photos_for_screensaver.scpt
Created March 19, 2022 18:49
Export modified (not original) photos from to a folder, to work around a screensaver bug.
View export_modified_photos_for_screensaver.scpt
-- Works around a bug where Photos screensavers show original rather than edited versions:
-- This script exports edited versions to a folder, which the screensaver can then be configured to use.
-- Run it on a schedule e.g. via crontab.
-- By Henrik Nyh <> under the MIT licence.
set albumName to "Skärmsläckare" -- Export from this album.
set exportFolder to POSIX file (do shell script "echo ~/Library/original_photos_for_screensaver") -- To this folder.
henrik /
Last active March 2, 2022 10:08
How to restore a downloaded backup dump

We had a downloaded MongoDB dump that we wanted to restore on This is how we did it:

  1. Download the backup via the MongoDB web UI.

  2. Unzip it. In this example we assume it unzipped to /tmp/foo.

  3. Have MongoDB installed locally, e.g. via:

     brew tap mongodb/brew
     brew install mongodb-community
  4. Start a local Mongo server:

henrik / honda_e_wallpaper.scpt
Last active October 8, 2021 20:08
Pixelmator Pro AppleScript to export Honda e wallpaper images. Details:
View honda_e_wallpaper.scpt
set currentTimestamp to do shell script "date +%s"
set exportFile to (path to desktop as text) & "honda_e_wallpaper_" & currentTimestamp & ".jpg"
tell application "Pixelmator Pro"
tell application "System Events" to tell process "Pixelmator Pro" to click menu item "Duplicate" of its menu of menu bar item "File" of menu bar 1
set sourceDoc to the front document
set targetDoc to make new document with properties {width:3840, height:720}