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 GrossWeight | |
| include Comparable | |
| attr_reader :value | |
| def initialize(value) | |
| @value = value | |
| end | |
| def <=>(other) |
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 'async' | |
| require 'async/barrier' | |
| require 'open-uri' | |
| def async_fetch(*urls) | |
| barrier = Async::Barrier.new | |
| Async do | |
| urls.map do |url| | |
| barrier.async { URI.open url, &:read } |
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
| Pry.config.prompt = Pry::Prompt[:simple] | |
| Pry.config.pager = false | |
| ## | |
| # Support refinements at top level in Pry. | |
| Pry.hooks.add_hook :when_started, :context_switch do |_output, _binding, pry| | |
| pry.binding_stack = [TOPLEVEL_BINDING] | |
| 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 StructLiteral | |
| refine Hash do | |
| def to_struct(class_name = nil) | |
| if class_name | |
| unless Object.const_defined?(class_name) | |
| Object.const_set(class_name, Struct.new(*keys)) | |
| end | |
| Object.const_get(class_name).new(*values) | |
| else |
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 recurse_ages(hash, depth: 0, history: []) | |
| hash.flat_map do |key, value| | |
| if value.instance_of?(Hash) | |
| history[depth] = key | |
| method(__method__).call value, depth: depth.succ, history: history | |
| else | |
| [history[0..depth.pred], value] | |
| 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
| h1 = { a: true, b: { c: [1, 2, 3], d: [42] } } | |
| h2 = { a: false, b: { x: [3, 4, 5], d: [42] } } | |
| h1.deep_intersection(h2) | |
| #=> ? |
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 Status | |
| include Comparable | |
| CODES = [['-', :todo], ['x', :doing], ['+', :done]].freeze | |
| SYM = CODES.map(&:first).each_with_index.to_h.freeze | |
| STR = CODES.map(&:last).each_with_index.to_h.freeze | |
| INT = CODES.each_with_index.reduce({}) do |acc, (keys, i)| | |
| acc.merge({i => i}, keys.to_h { |key| [key, i] }) | |
| end.freeze |
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 'securerandom' | |
| module RepeatedSample | |
| refine Array do | |
| def repeated_sample(sample_size = 1) | |
| m = 1 | |
| limit = size | |
| while limit * size <= 0x100000000 |
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
| @executor = Concurrent::ThreadPoolExecutor.new min_threads: min_threads, | |
| max_threads: max_threads | |
| def connection_pool | |
| @connection_pool ||= ConnectionPool.new size: @pool_size, timeout: @pool_timeout do | |
| HTTP.auth("Bearer #{access_token}") | |
| .persistent(HOST) | |
| .timeout(@http_timeout) | |
| .headers(accept_encoding: ACCEPT_ENCODING) | |
| .use(:auto_inflate) |
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 | |
| module Tracker | |
| FILENAME = 'tracking.txt' | |
| SEPARATOR = ': ' | |
| OPEN_SESAME = 'open' | |
| OPENING = "Opening %<item>s ...\n" | |
| module_function |