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
# 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 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 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 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 |
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 '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 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 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 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 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 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) |