View climate_control_helpers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ClimateControlHelpers | |
# Usage: | |
# | |
# describe Foo do | |
# stub_envs( | |
# FOO: "bar", | |
# ) | |
# | |
# it "uses the ENVs" | |
# end |
View send_im.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env osascript | |
# Required parameters: | |
# @raycast.schemaVersion 1 | |
# @raycast.title Send IM | |
# @raycast.mode silent | |
# Optional parameters: | |
# @raycast.icon 💬 | |
# @raycast.author Henrik Nyh |
View rspec_stub_env.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View sonos_sub_toggle.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View ruby_guard_clause_proof_of_concept.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View chunk_by_desired_max_value.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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| |
View bigdecimal_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "open-uri" | |
r = -> { URI(_1).read } | |
post_dates = r.("https://daringfireball.net/archive/") | |
.scan(%r{<small>(.+?)</small>}) | |
.map { |(x)| Date.parse(x.gsub(" ", " ")) } | |
link_dates = r.("https://daringfireball.net/linked/") | |
.scan(%r{href="(.+?/linked/20\d\d/.+?)"}) |
View attr_extras_using_blocks_proof_of_concept.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder