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 Bendford | |
| VARIED_DIGITS = 5 | |
| PLACES = [ | |
| [0, *(1..9).map { Math.log10(1.fdiv(_1) + 1) }], | |
| *(2..VARIED_DIGITS).map do |place| | |
| (0..9).map do |d| | |
| (10 ** (place - 2)..((10 ** (place - 1)) - 1)).sum do | |
| Math.log10(1 + 1.fdiv(10 * _1 + d)) | |
| 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 'async/http/internet' | |
| Async do | |
| internet = Async::HTTP::Internet.new | |
| response = internet.get('http://localhost:9292') | |
| stream = response.connection.stream | |
| stream.write('Hello World!') |
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 Integer | |
| def prime? | |
| return self >= 2 if self <= 3 | |
| if (bases = miller_rabin_bases) | |
| return miller_rabin_test(bases) | |
| 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_relative 'option' | |
| include Option | |
| # An integer division that doesn't `raise ZeroDivisionError` | |
| def checked_division(dividend, divisor) | |
| if divisor.zero? | |
| # Failure is represented as the `None` variant | |
| None[] | |
| 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
| require 'rubocop' | |
| require 'tempfile' | |
| class InlineRunner | |
| DEFAULT = RuboCop::Runner.new({}, RuboCop::ConfigStore.new) | |
| def initialize(runner: DEFAULT, filename: 'runner.rb') | |
| @runner = runner | |
| @file = Tempfile.new(filename) | |
| 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_relative 'kernel' | |
| struct :Point do | |
| { | |
| x: 0, | |
| y: 0, | |
| z: 0 | |
| } | |
| 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 'json' unless defined?(JSON::JSON_LOADED) && JSON::JSON_LOADED | |
| class Enumerator | |
| class ArithmeticSequence | |
| # See #as_json. | |
| def self.json_create(object) | |
| Range.new(*object.values_at('b', 'e', 'x')) % object['s'] | |
| 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 'fileutils' | |
| require 'tempfile' | |
| def write_atomically(content, temp_name: 'temp.txt', atomic_path: 'atomic.txt') | |
| temp = Tempfile.new temp_name | |
| temp.write content | |
| temp.close | |
| FileUtils.mv temp.path, atomic_path | |
| ensure |
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
| foo = :untouched | |
| bar = :untouched | |
| [:touched].each do |foo| | |
| bar = :touched | |
| end | |
| foo #=> :untouched | |
| bar #=> :touched |
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 'digest/md5' | |
| class_name = Digest::MD5 | |
| module class_name::Mixin | |
| end | |
| class_name::Mixin.class_eval do | |
| class_name.public_methods(false).each do |method_name| | |
| define_method method_name, class_name.public_method(method_name).to_proc | |
| end |