Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
@henrik
henrik / rspec_stub_env.rb
Last active July 26, 2023 14:48
Stub ENVs in RSpec.
def stub_env(key, value)
is_set_up_flag = "__stub_env_is_set_up"
unless ENV[is_set_up_flag]
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:[]).with(is_set_up_flag).and_return(true)
end
@henrik
henrik / sonos_sub_toggle.sh
Last active October 26, 2022 22:41
Bash script to toggle the Sonos subwoofer via soco-cli.
# 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
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
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`.
# 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
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
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.
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.
-- 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

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: