View yarb.rb
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 'zlib' | |
path = 'example.rb' | |
## | |
# Compile and cache Ruby binary to disk. | |
binary = RubyVM::InstructionSequence.compile_file(path).to_binary | |
binary_path = "#{path}.yarb.gz" | |
Zlib::GzipWriter.open(binary_path) do | |
_1.write(binary) |
View wombat.rb
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 'forwardable' | |
class Wombat | |
NO_DEFAULT = Object.new | |
include Comparable | |
include Enumerable | |
extend Forwardable | |
def_delegators :@list, :[], :[]=, :fetch, :<<, :delete, :size, :<=> |
View stream_closing.rb
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 'trenni/template' | |
## | |
# Streaming template | |
TEMPLATE = Trenni::Template.load(<<~'HTML') | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> |
View bendford.rb
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
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 |
View client.rb
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 'async/http/internet' | |
Async do | |
internet = Async::HTTP::Internet.new | |
response = internet.get('http://localhost:9292') | |
stream = response.connection.stream | |
stream.write('Hello World!') |
View integer_prime.rb
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
class Integer | |
def prime? | |
return self >= 2 if self <= 3 | |
if (bases = miller_rabin_bases) | |
return miller_rabin_test(bases) | |
end | |
View option.rb
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 | |
# An example of a Rust-like `Option` enum in Ruby | |
module Option | |
class NoneError < StandardError | |
def message = "called #unwrap on a `None' value" | |
end | |
Some = Data.define(:unwrap) do | |
def initialize(value) = super |
View inline_runner.rb
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 '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 |
View example.rb
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 'kernel' | |
struct :Point do | |
{ | |
x: 0, | |
y: 0, | |
z: 0 | |
} | |
end | |
View arithmetic_sequence.rb
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' | |
class Enumerator | |
class ArithmeticSequence | |
def to_json(*args) | |
{ | |
JSON.create_id => self.class.name, | |
'begin' => self.begin, |
NewerOlder