Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
@henrik
henrik / take1.rb
Last active December 2, 2023 08:35
Advent of Code day 1 part 2
# The no-frills take.
words = %w[ one two three four five six seven eight nine ]
hash = words.flat_map.with_index(1) { |word, index| [ [ word, index ], [ index.to_s, index ] ] }.to_h
puts DATA.readlines.sum { |line|
_, first_digit = hash.min_by { |string, _| line.index(string) || 999 }
_, last_digit = hash.max_by { |string, _| line.rindex(string) || -1 }
first_digit * 10 + last_digit
@henrik
henrik / app__lib__temporary_network_errors.rb
Created October 27, 2023 11:26
Ruby `TemporaryNetworkErrors.all` – sloppier superset of https://github.com/barsoom/net_http_timeout_errors.
require "net/ssh/proxy/errors"
module TemporaryNetworkErrors
def self.all
[
*NetHttpTimeoutErrors.all,
Errno::EBADF,
IOError,
OpenSSL::SSL::SSLError,
@henrik
henrik / config__initializers__exceptions.rb
Last active October 27, 2023 11:29
Sidekiq "silence_errors_during_retries".
# Intended for silent Sidekiq retries: https://www.mikeperham.com/2017/09/29/retries-and-exceptions/
class Exception
attr_accessor :ignore_in_error_reporting
end
module ClimateControlHelpers
# Usage:
#
# describe Foo do
# stub_envs(
# FOO: "bar",
# )
#
# it "uses the ENVs"
# end
@henrik
henrik / send_im.sh
Last active February 26, 2023 15:58
Raycast script command to send a Messages.app 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 💬
# @raycast.author Henrik Nyh
@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