Skip to content

Instantly share code, notes, and snippets.

View gr33n7007h's full-sized avatar
🌴
On vacation

Li gr33n7007h

🌴
On vacation
View GitHub Profile
@gr33n7007h
gr33n7007h / parse.rb
Created April 29, 2024 03:24
for zayd on #ruby
# parse lspci output
require 'shellwords'
def parse_lspci_output
IO.popen('lspci -m') do |io|
io.readlines.filter_map do |line|
if line.match?(/3D|VGA|Display/)
pci_vendor, pci_device = line.shellsplit[2..4]
"#{pci_vendor} #{pci_device}"
@gr33n7007h
gr33n7007h / skynet.rb
Created March 25, 2024 20:38
Skynet Benchmark
# https://github.com/atemerev/skynet
module Skynet
module_function
def make_fiber(num, size, div)
Fiber.new do
Fiber.yield(num) if size == 1
fibers = (0...div).map do |i|
sub = num + i * (size / div)
@gr33n7007h
gr33n7007h / 🤣.rb
Created October 17, 2023 20:32
Manipulate dialog colours in IRB
# save this peice of code in your ~/.irbrc
module Reline
DUMMY = ->() {
# autocomplete
return nil unless config.autocompletion
if just_cursor_moving and completion_journey_data.nil?
# Auto complete starts only when edited
return nil
end
@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
@gr33n7007h
gr33n7007h / global_variable_get.rb
Created June 9, 2023 04:28
read globals w/o eval
require 'fiddle'
module Kernel
fname = 'rb_gv_get'
if Fiddle::Handle::DEFAULT.sym_defined?(fname)
sym = Fiddle::Handle::DEFAULT.sym(fname)
fun = Fiddle::Function.new(sym, [:const_string], :voidp, name: fname)
define_method(fname, &fun)
end
@gr33n7007h
gr33n7007h / parse_positional.rb
Created September 14, 2022 09:43
AstrallyForged
require 'optparse'
Arguments = Struct.new(:copies, :filename)
arguments = Arguments.new
options = ARGV.options do |opt|
opt.banner = "usage: #{opt.program_name} [options] <input file>"
opt.on '--copies=n', 'Number of copies', &:to_i
opt.on '-h', '--help', 'Displays this usage screen' do
puts opt
#!/usr/bin/env lua
local std = require'posix.unistd'
local sio = require'posix.stdio'
local rd, wr = std.pipe()
std.write(wr, "foo\nbar\nbaz\nfood")
std.close(wr)
local file = sio.fdopen(rd, 'r')
@gr33n7007h
gr33n7007h / variadic.rb
Created November 16, 2020 22:50
Testing variadic Fiddle version 1.0.1
require 'fiddle'
include Fiddle
# Fiddle v:1.0.1
setlocale = Function.new(
Handle['setlocale'],
[
:int,
:const_string
@gr33n7007h
gr33n7007h / rstring.rb
Created November 16, 2020 21:38
Testing nested struct/union Fiddle version 1.0.1
require 'fiddle/import'
include Fiddle::Importer
# Fiddle v:1.0.1
dlload
typealias 'VALUE', 'unsigned long long'
RBasic = struct [
'VALUE flags',
@gr33n7007h
gr33n7007h / fdf.rb
Created July 20, 2020 03:46
Fast Doubling Fibonacci
def fdf(n)
return 1 if n <= 2
k = n >> 1
a = fdf(k + 1)
b = fdf(k)
n % 2 == 1 ? (a * a + b * b) : (b * (2 * a - b))
end