Skip to content

Instantly share code, notes, and snippets.

View 0x1eef's full-sized avatar

Robert 0x1eef

View GitHub Profile
@havenwood
havenwood / data_literal.rb
Created January 9, 2025 16:41
A `~{meaning: 42}`-style Data literal implemented in Ruby
module DataLiteral
refine Hash do
def to_data(name = nil)
if name
unless Object.const_defined?(name)
Object.const_set(name, Data.define(*keys))
end
Object.const_get(name).new(*values)
else
@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 9, 2025 16:30
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
unless Object.const_defined?(class_name)
Object.const_set(class_name, Struct.new(*keys))
end
Object.const_get(class_name).new(*values)
else
@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