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 / mirror_update.hook
Created April 11, 2020 08:46
Update mirrors.
[Trigger]
Operation = Upgrade
Type = Package
Target = pacman-mirrorlist
[Action]
Description = Updating pacman-mirrorlist with reflector and removing pacnew...
When = PostTransaction
Depends = reflector
Exec = /bin/sh -c "reflector --protocol https --country 'United Kingdom' --score 10 --sort rate --save /etc/pacman.d/mirrorlist; rm -f /etc/pacman.d/mirrorlist.pacnew"
>> puts RubyVM::InstructionSequence.compile('-2 ** 2').disasm
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,7)> (catch: FALSE)
0000 putobject 2 ( 1)[Li]
0002 putobject 2
0004 opt_send_without_block <calldata!mid:**, argc:1, ARGS_SIMPLE>
0006 opt_send_without_block <calldata!mid:-@, argc:0, ARGS_SIMPLE>
0008 leave
=> nil
>> puts RubyVM::InstructionSequence.compile('-2.itself ** 2').disasm
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,14)> (catch: FALSE)
require 'socket'
TCP_STATES = {
1 => :established,
2 => :syn_sent,
3 => :syn_recv,
4 => :fin_wait1,
5 => :fin_wait2,
6 => :time_wait,
7 => :close,
@gr33n7007h
gr33n7007h / download.rb
Created February 28, 2020 20:16
Memory efficient image download
require 'open-uri'
uri = URI("https://i.imgur.com/NYPjmMN.jpg")
File.open('/path/you/want/to/save/image/blah.jpeg', 'wb') do |dest|
URI.open(uri) do |source|
IO.copy_stream(source, dest)
end
end
@gr33n7007h
gr33n7007h / fizzbuzz.rb
Created February 7, 2020 01:01
FizzBuzz using Ruby 2.7 pattern matching.
#!/usr/bin/env ruby
fizzbuzz = -> (n) {
case [n % 3, n % 5, n]
in 0, 0, _; "FizzBuzz"
in 0, _, _; "Fizz"
in _, 0, _; "Buzz"
else n
end
}
require 'fiddle/import'
class Object
extend Fiddle::Importer
RUBY_FL_SINGLETON = 1 << 12
RBasic = struct ['uintptr_t flags', 'uintptr_t klass']
private_constant :RUBY_FL_SINGLETON, :RBasic
require 'yaml'
t = %i(name address tel fax email website).freeze
c = []
File.read('data.txt').split("\n\n").each do |line|
c << t.zip(line.split("\n")).to_h
end
puts c.to_yaml
@gr33n7007h
gr33n7007h / test.lua
Created June 5, 2019 17:35
Run Ruby from LuaJIT
#!/usr/bin/env luajit
local ffi = require 'ffi'
local cast = ffi.cast
local cdef = ffi.cdef
local load = ffi.load
local libruby = load '/usr/lib/libruby.so'
cdef[[
@gr33n7007h
gr33n7007h / callback.c
Last active April 5, 2019 09:44
Testing a C callback from Ruby
#include <stdio.h>
// compile: gcc -g -fPIC -Wall -Werror -Wextra -pedantic callback.c -shared -o libtest.so
void
message(void)
{
puts("wassupppp!");
}
@gr33n7007h
gr33n7007h / homer
Created March 23, 2019 13:44
c0mrade
,---. ,.'-. \ ( ( ,'"""""-. `,X `. /` ` `._ ( , ,_\ | ,---.,'o `. | / o \ ) \ ,. ( .____, \| \ \____,' \ '`'\ \ _,____,' \ ,-- ,-' \ ( C ,' \ `--' .' | | | .O | __| \ ,-'_ / `L `._ _,' ' `. / `--.._ `',. _\ ` `-. /\ | `. ( ,\ \ _/ `-._ / \ |--' ( \ ' `-. `' \/\`. `. ) \ \ `. | |