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 'fiddle' | |
require 'fiddle/import' | |
module GlobalVariable | |
extend Fiddle::Importer | |
dlload Fiddle::Handle::DEFAULT | |
extern 'void rb_gv_set(const char*, unsigned long)' | |
extern 'unsigned long rb_gv_get(const char*)' | |
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 'io/stream' | |
require 'open3' | |
module Command | |
Result = Data.define(:out, :err, :status) | |
def self.run(*, **) | |
Open3.popen3(*, **) do |stdin, stdout, stderr, thread| | |
stdin.close |
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 Document | |
def self.included(base) | |
base.class_eval do | |
def self.name = "#{super}ument" | |
end | |
end | |
module_function | |
def print = :printed |
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
signal = 'ALRM' | |
status = Signal.list.fetch(signal) + 2**7 | |
pid = fork do | |
trap signal do | |
warn 'Alarm!' | |
exit status | |
end | |
sleep |
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 'stringio' | |
require 'zlib' | |
io = StringIO.new('Zip me...') | |
zipped_io = StringIO.new('', 'a+b') | |
writer = Zlib::GzipWriter.new(zipped_io) | |
IO.copy_stream(io, writer) | |
writer.finish | |
zipped_io.rewind |
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
class IO | |
module UnbufferedPipe | |
refine IO.superclass do | |
def unbuffered_pipe(*, **) | |
IO.pipe(*, **) do |read_io, write_io| | |
read_io.sync = true | |
write_io.sync = true | |
yield read_io, write_io | |
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
#!/usr/bin/env -S zsh -e | |
help() { | |
print -u2 -- "Usage: $ZSH_ARGZERO [options] [directory...] | |
Options: | |
-h, --help Output this help message | |
-v, --version Output program name and version" | |
exit ${1:-0} | |
} |
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
#!/usr/bin/env -S zsh -e | |
zparseopts -D -F -- \ | |
g:=opt_gb -reserve-gb:=opt_gb \ | |
p:=opt_percent -reclaim-percent:=opt_percent \ | |
h=opt_help -help=opt_help \ | |
v=opt_version -version=opt_version | |
if (( ${#opt_help} )); then | |
print -- "Usage: $(basename $0) [options] |
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
# Caching dramatically improves performance when portions of the | |
# factorial calculation are repeated. | |
class UnaryFactorial < Module | |
class NegativeError < StandardError | |
def initialize(message = 'Factorials cannot be negative') = super | |
end | |
attr_accessor :cache | |
## |
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
class Ractor | |
module Channel | |
def self.new(&block) | |
channel = Ractor.new do | |
loop do | |
Ractor.yield Ractor.receive | |
end | |
end | |
return block.call(channel) if block_given? |
NewerOlder