Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
gr33n7007h / frozen_core.rb
Created September 12, 2023 15:08
Enable FrozenCore monkey-patching bytecode
# frozen_string_literal: true
abort 'MJIT needs to be enabled' unless RubyVM::MJIT.enabled?
# make private constants available at top-level
#
constants =
Symbol.all_symbols.select do
RubyVM::MJIT.private_constant _1 rescue next
end
@havenwood
havenwood / enumerable.rb
Last active April 24, 2022 20:26
Implementing #each with Ractor- and Async-based concurrency for fun
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)
@havenwood
havenwood / struct_literal.rb
Last active January 11, 2024 23:52
An anonymous Struct in pure Ruby (hoping Ruby adds a real one)
module StructLiteral
refine Hash do
def to_struct(class_name = nil)
if class_name
Object.const_set(class_name, Struct.new(*keys))
Object.const_get(class_name).new(*values)
else
Struct.new(*keys).new(*values)
end
end
@havenwood
havenwood / memoized.rb
Last active May 15, 2022 18:04
Enumerable#memoized so Enumerable#lazy doesn't get lonely
# frozen_string_literal: true
class Enumerator
class Memoized < Enumerator
INTERRUPT = defined?(IRB::Abort) ? IRB::Abort : Interrupt
private_constant :INTERRUPT
def initialize(enum)
@original_inspect = enum.inspect
@enum = cloned(enum)
#!/usr/bin/env python2
# coding=utf-8
from math import *
from time import sleep
import os
import random
import sys
write = sys.stdout.write