This file contains hidden or 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
# Written based on a conversation about enumerating with behavior for | |
# empty collections | |
# | |
# https://ruby.social/@pushcx/113397079757804801 | |
# https://www.justinweiss.com/articles/each-dot-dot-dot-or-else/ | |
module OtherwiseEnumerable | |
refine Enumerable do | |
def otherwise | |
return yield if empty? |
This file contains hidden or 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 Memoizer < Module | |
def initialize(method_name) | |
define_method(method_name) do | |
super().tap do |value| | |
puts "memoizing" | |
define_singleton_method(method_name) { value } | |
end | |
end | |
end | |
end |
This file contains hidden or 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
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem "rails", github: "rails/rails" |
This file contains hidden or 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 SpeedLimit | |
def initialize(enumerator) | |
@enumerator = enumerator.to_enum | |
end | |
def at(count, per:) | |
increment_seconds = case per | |
when :second then 1.0 / count.to_f | |
when :minute then 60.0 / count.to_f | |
when :hour then 3600.0 / count.to_f |
This file contains hidden or 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 level_descriptions=(input) | |
write_attribute(:levels_description, input).tap do |_result| | |
flush_cache :levels_description | |
end | |
end | |
# same number of lines, one less method call, one less block, but pretty much the same as | |
# far as I'm concerned. | |
def level_descriptions=(input) | |
result = write_attribute(:levels_description, input) |
This file contains hidden or 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 Memo | |
def memoize(method_name) | |
original_method = instance_method(method_name) | |
method_cache = Hash.new { |h, k| h[k] = {} } | |
define_method(method_name) do |*args, &block| | |
if method_cache[self].key?([args, block]) | |
method_cache[self][[args, block]] | |
else | |
method_cache[self][[args, block]] = original_method.bind(self).call(*args, &block) | |
end |
This file contains hidden or 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 Instrumenter | |
class Instrumentation < Module | |
def initialize(method, label) | |
@method = method | |
@label = label | |
define_method(method) do |*args| | |
start_time = Time.now | |
super(*args).tap do | |
end_time = Time.now |
This file contains hidden or 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
bathroom.urinals.occupied.select { |urinal| urinal.adjacent.select(&:occupied?).any? } | |
Urinal = Struct.new(:is_occupied, :location) { def adjacent_urinals; location.adjacent_urinals; end } |
This file contains hidden or 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 "delegate" | |
Foo = Class.new(SimpleDelegator) | |
Bar = Struct.new(:foo) | |
Foo.new(1) == Foo.new(1) # => true | |
Bar.new(1) == Bar.new(1) # => true | |
Foo.new(Bar.new(1)) == Foo.new(Bar.new(1)) # => false |
This file contains hidden or 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
# frozen_string_literal: true | |
require "delegate" | |
require "set" | |
# Hash that can only hold a certain number of keys, keys expire on a LRU basis | |
class BoundedHash < DelegateClass(Hash) | |
NO_OP = proc {} |
NewerOlder