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/queue' | |
| def skynet(c, num, size, div) | |
| if size == 1 | |
| c << num | |
| else | |
| rc = Async::LimitedQueue.new(div) | |
| sum = 0 | |
| div.times do |i| |
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 'kaprekar' | |
| include Kaprekar | |
| p kaprekar(four_digit_number: 9000).first(10) | |
| #>> [9000, 8991, 8082, 8532, 6174, 6174, 6174, 6174, 6174, 6174] | |
| p kaprekar(four_digit_number: 9000).take_while { _1 != Kaprekar::CONSTANT } | |
| #>> [9000, 8991, 8082, 8532] |
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 | |
| source 'https://rubygems.org' | |
| gem 'rack', github: 'rack/rack' | |
| gem 'puma', github: 'puma/puma' |
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 'pstore' | |
| class Store | |
| def initialize(relative_path: '.data.pstore') | |
| @store = PStore.new(relative_path, true) | |
| @store.ultra_safe = true | |
| @store.transaction(true) { } | |
| @in_transaction = false | |
| @store | |
| 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 'async' | |
| class Enumerator | |
| class Async | |
| module Refinement | |
| refine Enumerable do | |
| def async | |
| Enumerator::Async.new(self) | |
| 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
| require 'async' | |
| module Enumerable | |
| module Async | |
| [Array, Hash, Range, Struct].each do |collection| | |
| refine collection do | |
| def each(&block) | |
| super do |value| | |
| Async do | |
| block.call(value) |
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' | |
| module Task | |
| module_function | |
| def async(*method_names) | |
| Async do | |
| method_names.map do |job| | |
| Async do | |
| job.to_proc.call(self) |
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 'matrix' | |
| class TicTacToe | |
| BOARD_SIZE = 3 | |
| MARKS = {x: -1, o: 1}.freeze | |
| private_constant :MARKS | |
| ROW_MOVES = %i[top middle bottom].freeze | |
| private_constant :ROW_MOVES |
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 Undigits | |
| refine Array do | |
| def undigits(base = 10) | |
| reverse.reduce(0) do |acc, digit| | |
| acc * base + digit | |
| 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
| # When Math.sqrt(2).fdiv(2) is just too zippy. | |
| require 'bigdecimal' | |
| require 'bigdecimal/util' | |
| def i_ish(precision:) | |
| sqrt_2 = 2.to_d.sqrt precision | |
| sqrt_i = Complex sqrt_2 / 2, sqrt_2 / 2 | |
| sqrt_i * sqrt_i | |
| end |