Skip to content

Instantly share code, notes, and snippets.

Avatar
🦆
🦆 🦆 🦆 🦆

Henrik Nyh henrik

🦆
🦆 🦆 🦆 🦆
View GitHub Profile
@henrik
henrik / sonos_sub_toggle.sh
Last active October 26, 2022 22:41
Bash script to toggle the Sonos subwoofer via soco-cli.
View sonos_sub_toggle.sh
# Uses https://github.com/avantrec/soco-cli via https://pypa.github.io/pipx/.
# I map this to a HTPC keyboard key via Alfred.app.
if [ `soco -l "Front room" sub_enabled` == "on" ]; then
soco -l "Front room" sub_enabled off : "Front room" light off
say -vDaniel "Sub off"
else
soco -l "Front room" sub_enabled on : "Front room" light on
say -vDaniel "Sub on"
fi
View with_foo.rb
class Foo
private
def bar = "bar"
end
foo = Foo.new
p foo.methods.include?(:bar) # => false
p foo.method(:bar) # Method
p Foo.instance_method(:bar) # Method
@henrik
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… https://twitter.com/henrik/status/1570145637920026624
View ruby_guard_clause_proof_of_concept.rb
class Object
def guard(cond, meth)
prepend(Module.new {
define_method(meth) { |*a, **aa, &b|
super(*a, **aa, &b) if send(cond, *a, *aa, &b)
}
})
end
end
@henrik
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 self.call(enum, max, &block)
block ||= -> { _1 }
sum = 0
enum.chunk_while { |a, b|
@henrik
henrik / bigdecimal_extensions.rb
Last active August 3, 2022 13:55
BigDecimal.with_decimal_precision
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)
else
BigDecimal(value, precision)
end
View daring_fireball.rb
require "open-uri"
r = -> { URI(_1).read }
post_dates = r.("https://daringfireball.net/archive/")
.scan(%r{<small>(.+?)</small>})
.map { |(x)| Date.parse(x.gsub("&nbsp;", " ")) }
link_dates = r.("https://daringfireball.net/linked/")
.scan(%r{href="(.+?/linked/20\d\d/.+?)"})
@henrik
henrik / attr_extras_using_blocks_proof_of_concept.rb
Last active May 4, 2022 15:43
Proof of concept for a version of https://github.com/barsoom/attr_extras 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 = block.parameters.map(&:last)
arg_names.each do
attr_reader _1
private _1
end
define_method(:__attr_extras_init_block, &block)
@henrik
henrik / export_modified_photos_for_screensaver.scpt
Created March 19, 2022 18:49
Export modified (not original) photos from Photos.app 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:
-- https://discussions.apple.com/thread/7881820
--
-- 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 <https://henrik.nyh.se> 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
henrik / how_to_restore_a_downloaded_mongodb_com_backup_dump.md
Last active March 2, 2022 10:08
How to restore a downloaded MongoDB.com backup dump
View how_to_restore_a_downloaded_mongodb_com_backup_dump.md

We had a downloaded MongoDB dump that we wanted to restore on cloud.MongoDB.com. 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
henrik / honda_e_wallpaper.scpt
Last active October 8, 2021 20:08
Pixelmator Pro AppleScript to export Honda e wallpaper images. Details: https://www.hondaeforums.com/viewtopic.php?f=8&t=255&p=11087#p11087
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"
activate
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}