Skip to content

Instantly share code, notes, and snippets.


Henrik Nyh henrik

View GitHub Profile
View climate_control_helpers.rb
module ClimateControlHelpers
# Usage:
# describe Foo do
# stub_envs(
# FOO: "bar",
# )
# it "uses the ENVs"
# end
henrik /
Last active February 26, 2023 15:58
Raycast script command to send a IM to a fixed contact, or just switch to Messages if no argument is provided. Don't forget to customize `theEmail`.
#!/usr/bin/env osascript
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Send IM
# @raycast.mode silent
# Optional parameters:
# @raycast.icon 💬
# Henrik Nyh
henrik / rspec_stub_env.rb
Last active February 23, 2023 15:57
Stub ENVs in RSpec.
View rspec_stub_env.rb
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)
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)